fft(x,n) NO BUILT IN FUNCTION WHERE n > size(x)

9 views (last 30 days)
Hi
I'm looking for fft & ifft function code for fff(x,n) where n > size(x).
I found this here :
and it works well for all fft(x) & ifft(x) where n=size(x) but not for n>size(x)
function Xk = myfft(x)
N = length(x); % The fundamental period of the DFT
n = 0:1:N-1; % row vector for n
k = 0:1:N-1; % row vecor for k
WN = exp(-1i*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFT matrix
Xk = x * WNnk; % row vector for DFT coefficients
function x_hat = myifft(ftx)
N = length(ftx); % The fundamental period of the DFT
n = 0:1:N-1;
k = 0:1:N-1;
WN = exp(-1i*2*pi/N);
nk = n'*k;
WNnk = WN .^ (-nk); % IDFS matrix
x_hat = (ftx * WNnk)/N; % row vector for IDFS values
x_hat=real(x_hat);
How can I modify this code (or to take an other one) for my task?
Thanks!

Accepted Answer

AL
AL on 16 Mar 2023
To modify the existing code for the FFT function, you can use the following code:
function Xk = myfft(x,n)
N = length(x); % The fundamental period of the DFT
if N<n
x=[x,zeros(1,n-N)];
N=n;
end
n = 0:1:N-1; % row vector for n
k = 0:1:N-1; % row vecor for k
WN = exp(-1i2pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFT matrix
Xk = x * WNnk; % row vector for DFT coefficients
The modifications include passing an additional parameter 'n' to the function to specify the desired size of the output FFT, and checking if the length of input 'x' is less than 'n', in which case zero-padding is done to make the length equal to 'n'.
Similarly, to modify the existing code for the IFFT function, you can use the following code:
function x_hat = myifft(ftx,n)
N = length(ftx); % The fundamental period of the DFT
if N<n
ftx=[ftx,zeros(1,n-N)];
N=n;
end
n = 0:1:N-1; % row vector for n
k = 0:1:N-1; % row vecor for k
WN = exp(-1i2pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^(-nk); % IDFS matrix
x_hat = (ftx * WNnk)/N; % row vector for IDFS values
x_hat=real(x_hat);

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!