Thread Subject:
error : "Matrix dimensions must agree" at STFT code

Subject: error : "Matrix dimensions must agree" at STFT code

From: Tilemachos

Date: 30 Jun, 2013 12:05:11

Message: 1 of 8

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

Subject: error : "Matrix dimensions must agree" at STFT code

From: dpb

Date: 30 Jun, 2013 17:12:06

Message: 2 of 8

On 6/30/2013 7:05 AM, Tilemachos wrote:
...

> 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"
>
...

> ...I dont know how can i get rid of this error.
>

Your (or TMW's, I didn't check headers) wrapped lines so badly I didn't
try to read the code, but--

dbstop on error

That will bring up the debug prompt and you can investigate what's what
at that point.

Look first at

size(w)

and

size(xt)

Both need to be the same for a point-by-point multiplication to succeed.

Whatever it is that caused that not to be so is the culprit.

--

Subject: error : "Matrix dimensions must agree" at STFT code

From: asif khan

Date: 1 Jul, 2013 06:38:46

Message: 3 of 8

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

Subject: error : "Matrix dimensions must agree" at STFT code

From: Barry Williams

Date: 1 Jul, 2013 10:24:20

Message: 4 of 8

asif khan <asifkhanmohmd@gmail.com> wrote in message <3de1bc28-d8af-4233-a5e3-c48e5de27730@googlegroups.com>...
> 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

You don't mention the error you get when you run the code above, but at least one error should be:

??? Undefined function or variable "xtwz".

when the following line runs:

 Xtwz(:,1) = fft(xtwz); % because xtwz was never defined.

Barry

Subject: error : "Matrix dimensions must agree" at STFT code

From: asif khan

Date: 1 Jul, 2013 11:16:15

Message: 5 of 8

yes, it is

Xtwz(:,1) = fft(Xtwz);------> line 21

Subject: error : "Matrix dimensions must agree" at STFT code

From: Tilemachos

Date: 1 Jul, 2013 12:02:07

Message: 6 of 8

asif khan <asifkhanmohmd@gmail.com> wrote in message <77c3d288-5700-4c23-b645-982442833afb@googlegroups.com>...
> yes, it is
>
> Xtwz(:,1) = fft(Xtwz);------> line 21



I corrected the initial problem with the matrix dimentions of w and xt by inverting the matrix w (w=w') but now when i try to run the program i get:

"Warning: Integer operands are required for colon operator when used as
index
> In mystft at 9

Error using horzcat
Dimensions of matrices being concatenated are not consistent.

Error in mystft (line 19)
    xtwz = [xtw(Mo2+1:M), zp, xtw(1:Mo2)]; % windowed, zero padded"

where line 9 is: " zp = zeros(N-M,1); % zero padding (to be inserted) "

Subject: error : "Matrix dimensions must agree" at STFT code

From: Steven_Lord

Date: 1 Jul, 2013 14:24:48

Message: 7 of 8



"Tilemachos " <thlpap@hotmail.com> wrote in message
news:kqrr3v$jic$1@newscl01ah.mathworks.com...
> asif khan <asifkhanmohmd@gmail.com> wrote in message
> <77c3d288-5700-4c23-b645-982442833afb@googlegroups.com>...
>> yes, it is
>>
>> Xtwz(:,1) = fft(Xtwz);------> line 21
>
>
>
> I corrected the initial problem with the matrix dimentions of w and xt by
> inverting the matrix w (w=w') but now when i try to run the program i get:
>
> "Warning: Integer operands are required for colon operator when used as
> index
>> In mystft at 9

Your code made an assumption that's not true.

"Mo2 = (M-1)/2; % Assume M odd for simplicity here"

If M is even, Mo2 is not an integer value and so when you use it to try to
create an index vector, you receive that warning.

If you want to avoid needing to assume M's parity, check it using MOD and do
the right thing in the cases where it's odd and where it's even.

> Error using horzcat
> Dimensions of matrices being concatenated are not consistent.
>
> Error in mystft (line 19)
> xtwz = [xtw(Mo2+1:M), zp, xtw(1:Mo2)]; % windowed, zero padded"
>
> where line 9 is: " zp = zeros(N-M,1); % zero padding (to be
> inserted) "

Set a breakpoint on that line. When you reach the breakpoint, check the
sizes of each of the three terms you're concatenating. Make sure they have
the same number of rows. If not, fix it so they do.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: error : "Matrix dimensions must agree" at STFT code

From: Barry Williams

Date: 1 Jul, 2013 19:07:14

Message: 8 of 8

asif khan <asifkhanmohmd@gmail.com> wrote in message <77c3d288-5700-4c23-b645-982442833afb@googlegroups.com>...
> yes, it is
>
> Xtwz(:,1) = fft(Xtwz);------> line 21

This was what you posted previously:
> Xtwz(:,1) = fft(xtwz); % STFT for frame m

See the difference?

Barry

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix dimensions ... Tilemachos 30 Jun, 2013 12:09:11
stft Tilemachos 30 Jun, 2013 12:09:10
rssFeed for this Thread

Contact us