[SOLVED] Matlab reshape error

Status
Not open for further replies.

eldamar

Newbie level 5
Joined
Apr 24, 2014
Messages
10
Helped
2
Reputation
4
Reaction score
2
Trophy points
3
Visit site
Activity points
68
Hi everyone. I am trying to write matlab programme for audio compression using DCT method. Here, I posted some of the my code.
Code:
%function result = compress_dct(X, window, num_components, coeff_bits)
[X1 , f ]=wavread('samplepiano.wav');
window=35208;
num_components=100;
coeff_bits=10;
num_win = 48;
%new=numel(X1);
%disp(new);
X = reshape(X1, window, num_win); % reshape so each window is a row
Y = dct(X); % applies dct to each row
[a, I] = sort(abs(Y), 'descend');
I = I(1:num_components, :);
....
Matlab gives such an error when I try to execute this programme and I didn't understand why.Can you help me to fix this error,please? ( By the way, my wav file has 3379968 elements I found it with using numel(). )

Error using reshape
To RESHAPE the number of elements must not change.
 
Last edited by a moderator:

In your reshape operation, you are trying to create a (35208 x 48) matrix from a vector of length 3379968. Since 35208*48 = 1689984, this cannot be done.

If you only want to use the first 1689984 samples, you should write:
Code:
X = reshape(X1(1:window*num_win), window, num_win);

If you want to use all the data, you need to double num_win. In fact, since your data length divides exactly by 35208, a useful shortcut is to use "[]" for the other dimension:
Code:
X = reshape(X1, window, []);

Also, please note that your code comment is incorrect. In order to make each window a row, you must transpose the matrix (since reshape() operates column-wise):
Code:
X = reshape(X1, window, []).';
 
Last edited:
Thank you for your help, it is really helped me )

Regards eldamar...
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…