This document discusses applying the discrete Fourier transform (DFT) and fast Fourier transform (FFT) to analyze signals. It generates sine waves of different frequencies and lengths, applies various windowing functions like Bartlett, Hamming, and Hanning windows to the signals, and calculates the amplitude and power spectra using DFT and FFT. It displays the original and windowed signals in the time domain as well as their amplitude and power spectra in the frequency domain to analyze the effects of windowing.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
189 views
DSP FFT - Codigo en Matlab
This document discusses applying the discrete Fourier transform (DFT) and fast Fourier transform (FFT) to analyze signals. It generates sine waves of different frequencies and lengths, applies various windowing functions like Bartlett, Hamming, and Hanning windows to the signals, and calculates the amplitude and power spectra using DFT and FFT. It displays the original and windowed signals in the time domain as well as their amplitude and power spectra in the frequency domain to analyze the effects of windowing.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
DSP FFT
close all;clear all
% generate the sine wave sequence fs=8000; %Sampling rate N=1000; % number of data points x=2*sin(2000*pi*[0:1:N-1]/fs); % apply the DFT algorithm figure(1) xf=abs(fft(x))/N; %Compute the amplitude spectrum P=xf.*xf; %Compute power spectrum f=[0:1:N-1]*fs/N; %Map the frequency bin to frequency (Hz) subplot(2,1,1); plot(f,xf);grid xlabel('Frequency (Hz)'); ylabel('Amplitude spectrum (DFT)'); subplot(2,1,2);plot(f,P);grid xlabel('Frequency (Hz)'); ylabel('Power spectrum (DFT)'); figure(2) % convert it to one side spectrum xf(2:N)=2*xf(2:N); % Get the single- side spectrum P=xf.*xf; % Calculate the power spectrum f=[0:1:N/2]*fs/N % frequencies up to the folding frequency subplot(2,1,1); plot(f,xf(1:N/2+1));grid xlabel('Frequency (Hz)'); ylabel('Amplitude spectrum (DFT)'); subplot(2,1,2);plot(f,P(1:N/2+1));grid xlabel('Frequency (Hz)'); ylabel('Power spectrum (DFT)'); figure (3) % zero padding to the length of 1024 x=[x,zeros(1,24)]; N=length(x); xf=abs(fft(x))/N; %Compute amplitude spectrum with zero padding P=xf.*xf; %Compute power spectrum f=[0:1:N-1]*fs/N; %Map frequency bin to frequency (Hz) subplot(2,1,1); plot(f,xf);grid xlabel('Frequency (Hz)'); ylabel('Amplitude spectrum (FFT)'); subplot(2,1,2);plot(f,P);grid xlabel('Frequency (Hz)'); ylabel('Power spectrum (FFT)'); figure(4) % convert it to one-sided spectrum xf(2:N)=2*xf(2:N); P=xf.*xf; f=[0:1:N/2]*fs/N; subplot(2,1,1); plot(f,xf(1:N/2+1));grid xlabel('Frequency (Hz)'); ylabel('Amplitude spectrum (FFT)'); subplot(2,1,2);plot(f,P(1:N/2+1));grid xlabel('Frequency (Hz)'); ylabel('Power spectrum (FFT)');
USANDO VENTANAS close all;clear all % generate the sine wave sequence fs=8000; T=1/fs; % Sampling rate and sampling period x=2*sin(2000*pi*[0:1:50]*T); %generate 51 2000-Hz samples. % apply the FFT algorithm N=length(x); index_t=[0:1:N-1]; f=[0:1:N-1]*8000/N; %Map frequency bin to frequency (Hz) xf=abs(fft(x))/N; %Calculate amplitude spectrum figure(1) %using Bartlett window x_b=x.*bartlett(N)'; %Apply triangular window function xf_b=abs(fft(x_b))/N; %Calculate amplitude spectrum subplot(2,2,1);plot(index_t,x);grid xlabel('Time index n'); ylabel('x(n)'); subplot(2,2,3); plot(index_t,x_b);grid xlabel('Time index n'); ylabel('Triangular windowed x(n)'); subplot(2,2,2);plot(f,xf);grid;axis([0 8000 0 1]); xlabel('Frequency (Hz)'); ylabel('Ak (no window)'); subplot(2,2,4); plot(f,xf_b);grid; axis([0 8000 0 1]); xlabel('Frequency (Hz)'); ylabel('Triangular windowed Ak'); figure(2) % generate the sine wave sequence x=2*sin(2000*pi*[0:1:100]*T); %generate 101 2000-Hz samples. % apply the FFT algorithm N=length(x); index_t=[0:1:N-1]; f=[0:1:N-1]*fs/N; xf=abs(fft(x))/N; %using Hamming window x_hm=x.*hamming(N)'; %Apply Hamming window function xf_hm=abs(fft(x_hm))/N; %Calculate amplitude spectrum subplot(2,2,1);plot(index_t,x);grid xlabel('Time index n'); ylabel('x(n)'); subplot(2,2,3); plot(index_t,x_hm);grid xlabel('Time index n'); ylabel('Hamming windowed x(n)'); subplot(2,2,2);plot(f,xf);grid;axis([0 fs 0 1]); xlabel('Frequency (Hz)'); ylabel('Ak (no window)'); subplot(2,2,4); plot(f,xf_hm);grid;axis([0 fs 0 1]); xlabel('Frequency (Hz)'); ylabel('Hamming windowed Ak'); figure(3) % generate the sine wave sequence x=2*sin(2000*pi*[0:1:150]*T); % generate 151 2-kHz samples % apply the FFT algorithm N=length(x); index_t=[0:1:N-1]; f=[0:1:N-1]*fs/N; xf=2*abs(fft(x))/N;xf(1)=xf(1)/2; % single-sided spectrum %using Hanning window x_hn=x.*hanning(N)'; xf_hn=2*abs(fft(x_hn))/N;xf_hn(1)=xf_hn(1)/2; %single-sided spectrum subplot(2,2,1);plot(index_t,x);grid xlabel('Time index n'); ylabel('x(n)'); subplot(2,2,3); plot(index_t,x_hn);grid xlabel('Time index n'); ylabel('Hanning windowed x(n)'); subplot(2,2,2);plot(f(1:(N-1)/2),xf(1:(N-1)/2));grid;axis([0 fs/2 0 2]); xlabel('Frequency (Hz)'); ylabel('Ak (no window)'); subplot(2,2,4); plot(f(1:(N-1)/2),xf_hn(1:(N-1)/2));grid;axis([0 fs/2 0 2]); xlabel('Frequency (Hz)'); ylabel('Hanning windowed Ak');