Error using * (dft with the input recorded voice)
8 次查看(过去 30 天)
显示 更早的评论
I am making a program that plot the magnitude vs frequency for the recorded voice in WAV file.
This is my code for the DFT:
DFT:
[y, Fs] = audioread('voice.wav');
Xn = y;
n = 0:length(Xn);
N = length(Xn);
k = N';
x = Xn*exp((-1j*2*pi*k*n)/N);
But I get an error
Error using *
Inner matrix dimensions must agree.
Error in R5_Carbonel (line 9)
x = Xn*exp((-1j*2*pi*k*n)/N);
回答(1 个)
Walter Roberson
2016-9-18
Your Xn will be a column vector of variable length (if the input is mono), or possibly an array with two columns (if the input is stereo). Let that length be L, so you have either an (L x 1) or an (L x 2) array (or even more if there are more channels!)
In your expression exp((-1j*2*pi*k*n)/N), all of the items are scalars except for n . You have
n = 0:length(Xn);
Remember, length() of a non-empty array is the largest dimension, so length(Xn) is probably going to be the number of samples (called L, above), but if you have more channels than samples then it could be the number of channels instead. It is bad programming practice to leave that to chance.
Assuming your file has more samples than channels, 0:length(Xn) is going to be length(Xn)+1 long; using our notation above, that is L+1 . And you are creating a row vector so that is (1 by (L+1))
You have Xn*exp((-1j*2*pi*k*n)/N) so the dimensions for the * operation are going to be (L x 1) on the left and (1 x (L+1)) on the right if there is only one channel in the file. The 1 of the columns on the left would match the 1 of the rows on the right, and that would be valid, producing an (L x (L+1)) output matrix. That might not be what you want, but it would not be giving you the error you are seeing.
But suppose you have two channels in the file, an (L x 2) array. The 2 of the columns on the left would not match the 1 of the rows on the right of the * operation, and you would get the error message you see.
fft related operations should be applied to each channel independently.
You should also be going back to your definition of dft. fft is an infinite summation . dft is a finite summation -- but it is a summation. Where is your summation ?
2 个评论
Walter Roberson
2016-9-18
You very likely have two channels on the voice file. You should loop doing the fft of each of them at a time instead of trying to do both of them together.
x = Xn*exp((-1j*2*pi*k*n)/N);
is not doing a summation. You need to figure out what it is that you need to sum.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Measurements and Spatial Audio 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!