Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: error : "Matrix dimensions must agree" at STFT code
Replies: 7   Last Post: Jul 1, 2013 3:07 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
asif khan

Posts: 16
Registered: 6/24/13
Re: error : "Matrix dimensions must agree" at STFT code
Posted: Jul 1, 2013 2:38 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Sunday, June 30, 2013 5:35:11 PM UTC+5:30, Tilemachos wrote:
> Dear friends,
>
>
>
> I am very new to matlab so forgive my ignorance. I am trying to make a code for an STFT (Short Time Fourier Transform) and then apply it to a wav. file so that i can plot the spectogramm of this wav. file.
>
>
>
> Here is the code:
>
>
>
> function Xtwz = mystft(x, M, R, N, w)
>
> nframes = floor(length(x)/R)-1;
>
> Xtwz = zeros(N,nframes); % pre-allocate STFT output array
>
> M = length(w); % M = window length, N = FFT length
>
> zp = zeros(N-M,1); % zero padding (to be inserted)
>
> xoff = 0; % current offset in input signal x
>
> Mo2 = (M-1)/2; % Assume M odd for simplicity here
>
> for m=1:nframes
>
> xt = x(xoff+1:xoff+M); % extract frame of input data
>
> xtw = w .* xt; % apply window to current frame
>
> if (N-M)~=0
>
> xtwz = [xtw(Mo2+1:M); zp; xtw(1:Mo2)]; % windowed, zero padded
>
> else
>
> xtwz = xtw;
>
> end
>
> Xtwz(:,m) = fft(xtwz); % STFT for frame m
>
> xoff = xoff + R; % advance in-pointer by hop-size R
>
> end
>
> I input a small wav file, and I give values to M=100, R=50, N=1024, w=hamming(M), and x=data (from the input file). Then when i try to apply the function "mystft" I get the error : "Error using .* /Matrix dimensions must agree./ Error in mystft (line 16)/ xtw = w.*xt; % apply window to current frame"
>
>
>
> First of all I am not sure whether the values that i have given to M,R,N are right, and secondly I dont know how can i get rid of this error.
>
>
>
> Any help would be appreciated,
>
>
>
> Thanks in advance


check this....
x = rand(1,100);
M = 100;
R = 50;
N = 1024;
w = hamming(M);
nframes = floor(length(x)/R)-1;
Xtwz = zeros(N,nframes); % pre-allocate STFT output array
M = length(w); % M = window length, N = FFT length
zp = zeros(N-M,1); % zero padding (to be inserted)
xoff = 0; % current offset in input signal x
Mo2 = (M-1)/2; % Assume M odd for simplicity here
for m=1:nframes
xt = x(xoff+1:xoff+M);% extract frame of input data
xt = xt';
xtw = w .* xt; % apply window to current frame
if (N-M)~=0
Xtwz = [xtw(Mo2+1:M); zp; xtw(1:Mo2)]; % windowed, zero padded
else
Xtwz = xtw;
end
Xtwz(:,1) = fft(xtwz); % STFT for frame m
xoff = xoff + R; % advance in-pointer by hop-size R
end



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.