Problem 5.17 (a)

Nasser Abbasi

 

OUTPUT

 

» nma_ftdemo

 

  nma_ftdemo - Discrete Fourier transform demonstration program

 

»

 

 

 

 

 

 

 

 

 

 

 

 


CODE

 

% nma_ftdemo - Discrete Fourier transform demonstration program

clear all; help nma_ftdemo; % Clear memory and print header

 

% modifed by Nasser Abbasi from ftdemo to solve problem 5.17 (a)

 

%* Initialize the sine wave time series to be transformed.

numberOfPoints = [50 512 4096];

freqs = [0.2 0.2123 0.8];

tau   = 1;  % Time increment

 

two_pi = 2*pi;

 

for(M=1:length(freqs))

    freq=freqs(M);   

    y=[];

    t=[];

    f=[];

   

    for(i=1:length(numberOfPoints))

        N=numberOfPoints(i);

        t = (0:(N-1))*tau;              % t = [0, tau, 2*tau, ... ]

 

        for(k=1:N)

          y(k) = mod(two_pi*t(k)*freq , two_pi);

        end

 

        f = (0:(N-1))/(N*tau);          % f = [0, 1/(N*tau), ... ]

        yt = fft(y);

 

        %* Graph the time series and its transform.

        figure;

        plot(t,y);

        title(sprintf('Original time series, N=%d, fs=%1.4f',N,freq));

        ylabel('Amplitude');  xlabel('Time');

        figure;

        plot(f,real(yt),'-',f,imag(yt),'--');

        legend('Real','Imaginary  ');

        title(sprintf('Fourier transform, N=%d, fs=%1.4f',N,freq));

        ylabel('Transform');  xlabel('Frequency');

 

        %* Compute and graph the power spectrum of the time series.

        figure;

        powspec = abs(yt).^2;

        semilogy(f,powspec,'-');

        title(sprintf('Power spectrum (unnormalized). N=%d, fs=%1.4f',N,freq));

        ylabel('Power');  xlabel('Frequency');

    end

end