Aya2002
Advanced Member level 4
- Joined
- Dec 12, 2006
- Messages
- 1,140
- Helped
- 184
- Reputation
- 376
- Reaction score
- 117
- Trophy points
- 1,343
- Location
- Iraq
- Activity points
- 8,006
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
%% 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 xfrom 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')
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?