recursive fft function- return if function

조회 수: 6 (최근 30일)
Liron Sabatani
Liron Sabatani 2021년 5월 28일
답변: vidyesh 2024년 2월 22일
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

답변 (1개)

vidyesh
vidyesh 2024년 2월 22일
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

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by