recursive fft function- return if function

6 views (last 30 days)
Liron Sabatani
Liron Sabatani on 28 May 2021
Answered: vidyesh on 22 Feb 2024
Hi:)
I wrote an recursive fft function and I used "if" function for the stop condition.
The start of the vector I got is good but I get N more members that come from the if loop that I must return a value there.
I would like to just end the while loop without return nothing.
example:
  • length(x)=N
  • fft(x)=[n1,...nN]
  • myrecfunc(x)=[n1,...nN,1,..,N] <--- the end is unnecessary:(
my func:
function Out = REC_FFT(x,C)
if C==0
Out=x;
else
N=length(x);
k=N-C;
sum=0;
for n=0:1:N/2-1
if mod(k,2)==0
sum=sum+(x(n+1)+x(n+1+floor(N/2)))*exp(-1i*(k/2)*n*(4*pi/N));
else
sum =sum+(x(n+1)-x(n+1+floor(N/2)))*exp(-1i*((k-1)/2)*n*(4*pi/N))*exp(-1i*n*(2*pi/N));
end
end
Out=[sum, REC_FFT(x,C-1)];
end
end
Please help me:)
Thank you very much
Liron

Answers (1)

vidyesh
vidyesh on 22 Feb 2024
Hi Liron,
It looks like your recursive FFT function is almost correct, but it's currently appending extra values at the end of your output. The root cause is likely the base case in your recursion, where you're returning ‘x’ instead of an empty array.
To resolve this, we can change the line ‘Out = x;’ in the final recursive call (where C equals 0) to ‘Out = [];’. This will ensure that when the recursion hits the base case, it won't return any additional elements, thus ending the recursion without appending unwanted values.
Hope this helps

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!