Need FFT Code for Matlab (not built in)
101 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Does anyone have FFT code for Matlab? I don't want to use the built-in Matlab function.
5 comentarios
Yasin Arafat Shuva
el 19 de Jul. de 2021
Editada: Rik
el 19 de Jul. de 2021
clear variables
close all
%%Implementing the DFT in matrix notation
xn = [0 2 4 6 8]; % sequence of x(n)
N = length(xn); % 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 = xn * WNnk; % row vector for DFT coefficients
disp('The DFT of x(n) is Xk = ');
disp(Xk)
magXk = abs(Xk); % The magnitude of the DFT
%%Implementing the inverse DFT (IDFT) in matrix notation
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 = (Xk * WNnk)/N; % row vector for IDFS values
disp('and the IDFT of x(n) is x_hat(n) = ');
disp(real(x_hat))
% The input sequence, x(n)
subplot(3,1,1);
stem(k,xn,'linewidth',2);
xlabel('n');
ylabel('x(n)')
title('plot of the sequence x(n)')
grid
% The DFT
subplot(3,1,2);
stem(k,magXk,'linewidth',2,'color','r');
xlabel('k');
ylabel('Xk(k)')
title('DFT, Xk')
grid
% The IDFT
subplot(3,1,3);
stem(k,real(x_hat),'linewidth',2,'color','m');
xlabel('n');
ylabel('x(n)')
title('Plot of the inverse DFT, x_{hat}(n) = x(n)')
grid
Respuestas (4)
Youssef Khmou
el 15 de Mzo. de 2013
hi John
Yes there are many versions of Discrete Fourier Transform :
% function z=Fast_Fourier_Transform(x,nfft)
%
% N=length(x);
% z=zeros(1,nfft);
% Sum=0;
% for k=1:nfft
% for jj=1:N
% Sum=Sum+x(jj)*exp(-2*pi*j*(jj-1)*(k-1)/nfft);
% end
% z(k)=Sum;
% Sum=0;% Reset
% end
% return
this is a part of the code , try my submission it contains more details :
4 comentarios
Abdullah Khan
el 28 de Nov. de 2013
Editada: Abdullah Khan
el 28 de Nov. de 2013
This is also DFT
The complexity of the code is still N x N
because
% nfft=2^ceil(log2(N)) = nfft (on upper DTFT code )
or
% 2^ceil(log2(N))= N
Walter Roberson
el 6 de Sept. de 2017
"FFT" stands for "Fast Fourier Transform", which is a particular algorithm to make Discrete Fourier Transform (DFT) more efficient.
Tobias
el 11 de Feb. de 2014
Editada: Walter Roberson
el 6 de Sept. de 2017
For anyone searching an educating matlab implementation, here is what I scribbled together just now:
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
4 comentarios
Marco Marcon
el 28 de Abr. de 2024
The total length of the final X will be N.
The first
values will be given by
+
(remember that
,
and
have length
elements and both
,
are periodic of
); it can be written as
for
, where
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681286/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681291/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681296/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681301/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681306/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681311/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681316/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681321/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681326/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681331/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681336/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681341/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681346/image.png)
While the last
values will be given by
for
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681351/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681356/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681361/image.png)
Since
we can get the second part of X(last
values) as:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681366/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681371/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1681376/image.png)
For more details: Wikipedia: Cooley-Tukey algorithm
Jan Afridi
el 6 de Sept. de 2017
Editada: Jan Afridi
el 6 de Nov. de 2017
I think I can answer your question. Check the given link may be it will help you. And if you found any error then please inform me... check the link Matlab code for Radix 2 fft
1 comentario
Jan Suchánek
el 6 de Dic. de 2018
Hello i would like to ask you what had to be different if you would like to use it for harmonical function.
I tried to implement your solution but it was working only for random vectors but not for any type of harmonical.
i was using this for the harmonical function.
______________________
t2=0:1:255;
h1=sin(t2);
x=h1;
X11 = myFFT(x);
______________________
it gave 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);
Error in ZBS_projekt (line 246)
X11 = myFFT(x);
Ryan Black
el 11 de Mzo. de 2019
Editada: Ryan Black
el 26 de Ag. de 2020
Function I wrote that optimizes the DFT or iDFT for BOTH prime and composite signals...
The forward transform is triggered by -1 and takes the time-signal as a row vector:
[Y] = fftmodule(y,-1)
The inverse transform auto-normalizes by N, is triggered by 1 and takes the frequency-signal as a row vector:
[y] = fftmodule(Y,1)
Methodology:
The algorithm decimates to N's prime factorization following the branches and nodes of a factor tree. In formal literature this may be referred to as Mixed Radix FFT, but its really just recursive decimation of additive groups and this method is easily derivable via circular convolutions :)
At the prime tree level, algorithm either performs a naive DFT or if needed performs a single Rader's Algorithm Decomposition to (M-1), zero-pads to power-of-2, then proceeds to Rader's Convolution routine (not easy to derive). Finally it upsamples through the origianlly strucutured nodes and branches incorporating twiddle factors for the solution.
0 comentarios
Ver también
Categorías
Más información sobre Discrete Fourier and Cosine Transforms en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!