2

I have found and customized algorithm for fast fourier transform. The algorithm is below:

function X = myFFT(x)
   %only works if N = 2^k
   N = numel(x);
   xp = x(1:2:end);
   xpp = x(2:2:end);
   if N>=8
      Xp = myFFT(xp);
      Xpp = myFFT(xpp);
      X = zeros(N,1);
      Wn = exp(-1i*2*pi.*((0:N/2-1)')/N);
      tmp = Wn .* Xpp;
      X = [(Xp + tmp);(Xp -tmp)];
   else
     switch N
       case 2
          X = [1 1;1 -1]*x;
       case 4
          X = [1 0 1 0; 0 1 0 -1i; 1 0 -1 0;0 1 0 1i]*[1 0 1 0;1 0 -1 0;0 1 0 1;0 1 0 -1]*x;
       otherwise
          error('N not correct.');
     end
   end
end

The algorithm above is stored in an .m file named myFFT.

I want to use the code for two subsequent signals: h1=sin(t2) and for time t2=0:1:255;

I recall the myFFT function in another .m file in this way:

x=h1;
X11 = myFFT(x);

But it gives me this error:

Error using  * 
Inner matrix dimensions must agree.

Error in myFFT (line 18)
           X = [1 0 1 0; 0 1 0 -1i; 1 0 -1 0;0 1 0 1i]*[1 0 1 0;1 0 -1 0;0 1 0 1;0 1 0 -1]*x;

Error in myFFT (line 7)
    Xp = myFFT(xp);

Error in myFFT (line 7)
    Xp = myFFT(xp);

Error in myFFT (line 7)
    Xp = myFFT(xp);

Error in myFFT (line 7)
    Xp = myFFT(xp);

Error in myFFT (line 7)
    Xp = myFFT(xp);

Error in myFFT (line 7)
    Xp = myFFT(xp);

Honestly, I have no idea how to fix it. Could you please help me? Thank you very much in advance!

Dalton Cézane
  • 3,672
  • 2
  • 35
  • 60
  • Try with `X11 = myFFT(x(:));`, flattening `x` to a column vector – Brice Dec 06 '18 at 17:16
  • 1
    @Brice: That flattening should happen inside the `myFFT` function. – Cris Luengo Dec 06 '18 at 17:36
  • @Chris Luengo : Perhaps. But then, there should be a clear specification of what the function is supposed to do (what kind of data in; what kind of data out). Should row vectors as input trigger an error, be processed normally as you suggest, or considered as N independent row vectors of size 1? – Brice Dec 07 '18 at 09:40

1 Answers1

2

The issue is the multiplication of a 4x4 to a 1x4 matrix. The variable x need to be transposed.

   function X = myFFT(x)
       %only works if N = 2^k
       N = numel(x);
       xp = x(1:2:end);
       xpp = x(2:2:end);
       if N>=8
          Xp = myFFT(xp);
          Xpp = myFFT(xpp);
          X = zeros(N,1);
          Wn = exp(-1i*2*pi.*((0:N/2-1)')/N);
          tmp = Wn .* Xpp;
          X = [(Xp + tmp);(Xp -tmp)];
       else
         switch N
           case 2
              X = [1 1;1 -1]*x';
           case 4
              X = [1 0 1 0; 0 1 0 -1i; 1 0 -1 0;0 1 0 1i]*[1 0 1 0;1 0 -1 0;0 1 0 1;0 1 0 -1]*x';
           otherwise
              error('N not correct.');
         end
       end
    end

then test it with

t2 = 0:1:255;
myFFT(sin(t2));
farbiondriven
  • 2,450
  • 2
  • 15
  • 31
  • It gives me the same error and yes, x is a 1x4 matrix for N = 4 – Franta Mráz Dec 06 '18 at 15:53
  • remember to put the ' , transpose operator on the x variable ! You are multiplying a 4x4 to a 1x4, that's why it failed – farbiondriven Dec 06 '18 at 16:00
  • I did it, and it still gives me the same error. You can test it on your own, it will give you the same error. – Franta Mráz Dec 06 '18 at 16:00
  • try to call myFFT([1 2 3 4]) to test. I just run it – farbiondriven Dec 06 '18 at 16:02
  • 3
    `'` is the ctranspose operator, the transpose operator is `.'` ! – obchardon Dec 06 '18 at 16:17
  • but sill your answer should work, since `x` is not a complex array. – obchardon Dec 06 '18 at 16:28
  • It gives me this error: Error using * Inner matrix dimensions must agree. Error in myFFT (line 18) X = [1 0 1 0; 0 1 0 -1i; 1 0 -1 0;0 1 0 1i]*[1 0 1 0;1 0 -1 0;0 1 0 1;0 1 0 -1]*x; Error in zkouska (line 16) X=myFFT([1 2 3 4]); – Franta Mráz Dec 06 '18 at 16:31
  • 1
    @FrantaMraz, have you read the answer of farbiondriven ? If you do not change your code, it's not really a suprise to get the same error. You still have not transposed the x array. – obchardon Dec 06 '18 at 16:35
  • I did it as he has written, and it still gives me an error I posted in my previous comment. – Franta Mráz Dec 06 '18 at 16:43
  • Just read the error of your previous comment and tell me where you see the `x.'`. – obchardon Dec 06 '18 at 16:47
  • There is nothing to change in the initial code; the function however is meant to work on column vectors (and does not have any input checking on that). `myFFT(sin(t2(:)));` should work. – Brice Dec 06 '18 at 17:14