% This MATLAB script is a template to % (1) define and plot a sinusoidal sequence, x[n], n=1..N % (2) calculate and plot its discrete-time Fourier series (DTFS) X[k], k=1..N by means of the fft algorithm. % % Author: A. Muschinski % Date: 4 March 2007 % ------------------------------------------------------------------------ % Clear workspace: clear all % --- Define DT signal x[n]: % N1 = period of sinusoid; % M = number of full oscillations; % P = N - M*N1 (number of "excess samples", that is, number of samples % in excess of full periods); % N = total number of samples. N1 = 50; M = 5; P = 1; N = M*N1+P; for i = 1:N n(i) = i-1; x(i) = sin((2*pi/N1)*i); end % Define k array equal to n array (k = 0,..,N-1): k = n; % --- Calculate the DTFS by means of the MATLAB Fast Fourier Transform % (FFT) algorithm: X = fft(x); mag_X = abs(X); phi_X = angle(X); %%%%%%%%%%%%%%% PLOT DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %------------------------------------------------- % Plot DT signal x: figure(1) plot(n,x,'k.-','MarkerSize',12) hold on plot([0 N+1],[0 0],'k-') hold off title('Discrete-time signal') xlabel('n') ylabel('x[n]') xlim([0 N+1]) ylim([-1.2 1.2]) %------------------------------------------------- % Plot absolute value of DTFS (semilogarithmically): figure(2) semilogy(k,mag_X,'k.-','MarkerSize',12) hold on plot([0 N+1],[0 0],'k-') hold off title('Magnitude of DTFS') xlabel('k') ylabel('|X[k]|') xlim([0 N+1]) % ylim([-0.2 1.2]) %-------------------------------------------------- % Plot absolute value of DTFS (linearly): figure(3) plot(k,mag_X,'k.-','MarkerSize',12) hold on plot([0 N+1],[0 0],'k-') hold off title('Magnitude of DTFS') xlabel('k') ylabel('|X[k]|') xlim([0 N+1]) % ylim([-0.2 1.2]) %-------------------------------------------------- % Plot phase of DTFS: figure(4) plot(k,phi_X,'k.','MarkerSize',12) hold on plot([0 N+1],[0 0],'k-') hold off title('Phase of DTFS') xlabel('k') ylabel('\phi_x[n]') xlim([0 N+1]) % ylim([-0.2 1.2])