Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to write FFT code in Matlab

Status
Not open for further replies.

amandeol

Newbie level 3
Joined
Mar 17, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,304
HELP .. how to write fft code in matlab .. not using the fft source funtion.. thanks in advance
 

fft code in matlab

This is a program in Matlab to do the DFT and the FFT (2-sided and 1-sided)

Good luck
%FFT lecture.
clc;close all; clear all;
%Generate the sine wave sequences
fs=8000; % sampling rate
N=1000; % Number of data points.
x=2*sin(2000*pi*[0:1:N-1]/fs) ;
figure(1) ;
stem( 1 : 1:N/10,x( 1 : 1 :N/10 ) ,'filled' ) ;%plot first 100 sample of x(n)
%% Apply the DFT algorithm
figure(2) ;
xf=abs(fft(x) )/N;% compute the amplitude spectrum
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N-1]*fs/N;%fk=k fs/N where k=0,1,2,...N-1
% now we will plot the DFT spectrums
subplot(211);plot(f,xf);grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (DFT) ' ) ;
subplot(212);plot(f,P);grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (DFT) ' ) ;
%% Convert it to one sided spectrum
figure(3);
xf(2:N)=2*xf(2:N);%Get the single side spectrum
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N/2]*fs/N;%frequency up to the folding frequency fs/2
% now we will plot the DFT spectrums
subplot(211);plot(f,xf(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (DFT)');
title('One Sided-Spectrum')
subplot(212);plot(f,P(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (DFT)');
title('One Sided-Spectrum')
%% Zero Padding to the length of 1024
figure(4);
x=[x,zeros(1,24)];%24 zero to extend the sequence x(n) from 1000 to 1024
N=length(x);
xf=abs(fft(x))/N; %compute the amplitude spectrum with zero padding
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N-1]*fs/N;%fk=k fs/N where k=0,1,2,...N-1
subplot(211);plot(f,xf);grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (FFT)');
subplot(212);plot(f,P);grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (FFT)');
%% Convert it to one sided spectrum
figure(5);
xf(2:N)=2*xf(2:N);%Get the single side spectrum
P=xf.*xf;%compute the power spectrum
% Map the frequency bin to the frequency (hz)
f=[0:1:N/2]*fs/N;%frequency up to the folding frequency fs/2
% now we will plot the FFT spectrums
subplot(211);plot(f,xf(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Amplitude Spectrum (FFT)');
title('One Sided-Spectrum')
subplot(212);plot(f,P(1:N/2+1));grid
xlabel('Frequency (Hz)');ylabel('Power Spectrum (FFT)');
title('One Sided-Spectrum')
 

    V

    Points: 2
    Helpful Answer Positive Rating
Re: fft code in matlab

Thanks for reply , but in this program u r using FFT function , which I m nt supposed to use , I wanna to write program which use mathematcial formula of converting the Time domain signal into frequency domain using matlab.
 

Re: fft code in matlab

dear colleague, first I apologize because my English is not very good;

Below is a matlab code that I created to calculate fft.
At the end of my code I compared with the fft matlab to check the differences are minimal.

I hope I have helped you

FYI:

Adriano



%%Algoritimo para calcular fft em matlab

%N é a quantidade de pontos da minha função de entrada e X é minha entrada
%Ex: X=[1 1 1 1 1] N=5 e 0<=r<=N-1
clear all
close all
clc

N = 40;

n = [1:N-1];
%X = [1 15 5 67 -9 8 7 6 8 1 2 4 5 76 89 90 56 54 345 43 45 43 34 56 78 98 76 54 34 23 n];
%X=tan(sin(2*pi*n/10));
X = sin(2*pi*n/10);
%X=exp(sin(2*pi*n/10));
%cálculo de G
G = zeros(N,1);
H = zeros(N,1);
nw = N/2;
%p=((N-1)/2)

mm = n(1,(N-1))/2;

%mm = p;


for r = 1:N;
for n = 1:mm;
%cálculo de w

w(n) = exp((-1i*2*pi*(r-1)*(n-1))/(N/2));
wn(r)=exp((-1i*2*pi*(r-1))/(N));
%cálculo de G
xg(n) = X(2*n);
G(r) = (G(r)+(w(n)*xg(n)));

%cálculo de H
xh(n) = X(2*n+1);
H(r) = (H(r)+(w(n)*xh(n)));

Xfft(r) = ([G(r)+(wn(r)*H(r))]);

n;
end
r;
end



% F = [-N/2:N/2-1]/N;
F = [0 : N - 1]/N;

y = abs(fft(X,N));

%%
u=ifft(y,N);
t=ifft(Xfft);


figure(1)
plot (F,abs(Xfft),'-x')
grid

figure(2)
plot(F,y,'-x')
grid

figure(3)
plot(X,'-x')
grid


figure(4)
plot(F,abs(u),'-o')
grid


figure(5)
plot(F,abs(t),'-o')
grid
 
  • Like
Reactions: Orji

    V

    Points: 2
    Helpful Answer Positive Rating

    Orji

    Points: 2
    Helpful Answer Positive Rating
Re: fft code in matlab

Hi Adraino,

I found you post very useful, and infact it is not difficult to understand what you mean. However, I need one more help. I have a data in frequency-space domain and I want to transform it to frequency wavenumber domian by writing the fourier transform for the space domain part of the data. I am encountering problem with it, do you have further idea?

The Pseudo code is like this

close all; clear all; clc;

Dat = load('filename'); % dat is in t-x domain;

Datw = fft(Dat); % Datw is in w-x domain

Now I want to write a code that transforms the space part from space to wavenumber leaving the data in w-k domain. You can reply in Portugese I can translate it in google.

Thanks
 

see is it useful to u or not

function [Xk]=dft(xn,N)

n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n’*k;
WNnk=WN.^nk;
Xk=xn*WNnk;
 

Hi Bisen,

Thanks for your help. However, it does not address my current problem.
 

Re: fft code in matlab

hi, Adraino.
i have one question here . i need help very much,..please help me...

Understanding the DFT by writing programs using Matlab. Used the Matlab program to
a) write a FFT program.
b) write a DFT program.
c) Compare the processing speed of FFT to DFT.
d) Compare the processing speed of the FFT program you write with the FFT program available in matlab. Which one is faster? Give the reasons for it.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top