1. Engineering
  2. Computer Science
  3. study the provided matlab programs entitled fftspectrum plotfftspectrum and testfftspectrum...

Question: study the provided matlab programs entitled fftspectrum plotfftspectrum and testfftspectrum...

Question details

Study the provided MATLAB programs entitled fft_spectrum, plot_fft_spectrum, and test_fft_spectrum. Using the program test_fft_spectrum, plot the approximate magnitude and phase spectra of the functions given in problem 3.2-1. Adjust your time axis so that the frequency plots are scaled conveniently, and important features of the time and frequency plots are clearly visible. This can be done by experimenting with the values of N, tmin, and tmax.

And the 3.2-1 problems are:

(a) pi(t/3) (b) Delta (3x/100) (c) pi (t-5/4) (d) sinc [ (2pif - 10pi)/5] (e) sinc (pit/5); (f) sinc (pit/5 pi(t/10)

**************************************************************************************
MATLAB programs entitled fft_spectrum

function x=sinc_nopi(t)
%The built-in matlab sinc function uses the standard definition of
% sinc(x) = sin(pi*x)/(pi*x).
%In the Lathi textbook, the sinc function is defined as:
% sinc(x) = sin(x)/x.
%This function implements the Lathi version of the sinc function, the
%difference being a scaling factor of pi on the t axis
%The traditional sinc function has zero-crossings at integers (except 0)
%The Lathi defined sinc function has zero crossings at multiples of pi

x=zeros(size(t));
x(t~=0)=sin(t(t~=0))./t(t~=0);
x(t==0)=1;

**************************************************************************************
MATLAB programs entitled test_fft_spectrum


function test_fft_spectrum
%this function calls fft_spectrum to compute the fft of an arbitrary
%signal, and plot the magnitude and phase spectrum
%the input signal, x may be either real or complex valued, and is created
%by inserting your own code after line 17

%INPUTS to set
%set N, the number of samples
N=256;
%set tmin and tmax, tmin should either be -tmax or 0
tmax=20; tmin=-20;

%create the time increment and the time axis
tinc=(tmax-tmin)/N;
t=tmin:tinc:(tmax-tinc);

%INSERT CODE HERE TO CREATE THE SIGNAL x(t)
%some examples are included here and commented out
%x=rectpuls(t-5/4);
%x=tripuls(3*t/100);
x=sinc_nopi(t);
%x=sin(2*pi*16*t);
%x=exp(-t.^2*32);

[f,X] = plot_fft_spectrum(t,x,1);

E1=sum(x.^2)*tinc
finc=f(2)-f(1);
E2=sum(abs(X).^2)*finc

**************************************************************************************

MATLAB programs entitled plot_fft_spectrum

function [f,X] = fft_spectrum(t,x)
%this function uses the built-in fft to compute the frequency spectrum of a
%signal x. It also generates the appropriate frequency scale, f, and
%applies the "fftshift" so that X and f vectors are available directly for
%plotting.

%the inputs x and t should be vectors of the same length.
%the outputs, X and f will also be vectors of that length

%this program is very simple, and will only work if the time axis either starts with 0, or is
%balanced positive and negative, with 0 in the center.

%extract the parameters of t vector
N=length(x);
dt=t(2)-t(1);
%create frequency vector
df=1/(N*dt);
%note the difference between odd length and even length vectors
if rem(N,2)==0 %N even
f= ( (-N/2):(N/2-1) )*df;
else %N odd
f= ( (-(N-1)/2):((N-1)/2) )*df;
end
%find the index of the t=0 element
N0=find(t==0);

%do the fft to create the spectrum samples
%use proper shifting depending on the time scale and scale amplitude by dt
if N0==1 %time scale is nonnegative
%X=fftshift(fft(fftshift(x)))*dt;
X=fftshift(fft((x)))*dt;
elseif rem(N,2)==0 && N0==N/2+1 %time scale is balanced, N is even
X=fftshift(fft(x))*dt;
elseif rem(N,2)==1 && N0==(N+1)/2 %time scale is balanced, N is odd
X=fftshift(fft(x))*dt;
else %time scale is unbalanced, so this function won't handle it
disp('Unbalanced time scale.')
end

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution