THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering EIE2106 Signal and System Analysis Lab 2 Fourier series 1. Objective The goal of this laboratory exercise is to explore the implementation of various fundamental concepts in Fourier analysis using MATLAB. The lab is divided into two parts: (i) Sinusoids and (ii) Fourier series. 2. Preparation A PC with Microsoft Windows 7 SP1, 8 or 8.1 MATLAB 2009b A headphone 3. Sinusoids Sinusoidal signals, or more concisely, sinusoids, are the most basic signals in signal processing. It can be represented mathematically by the following formulation: where A is the amplitude of the sinusoid; f is the frequency and is the phase delay. By using MATLAB, we can visualize a sinusoid. For example, the following statements let us display a sinusoid with A = 2, f =1000Hz and = /2. A=2; f0 = 1000; phi = pi/2; T0 = 1/f0; tt = 0 : T0/40 : 4*T0; xx = A*cos(2*pi*f0*tt + phi); plot(tt,xx) axis([0,0.004,-4,4]) and y axes xlabel('time (sec)'); grid on grid % Define the values the three parameters % Define the sampling points % Generate the cosine function % Plot the cosine function % Adjust maximum and minimum of x % Add a label to x axis % Show the cosine function on a 3.1 Try the program above by putting it into a M file. Show the result waveform. 3.2 Modify the program such that it would only show 3 cycles of the sinusoid. Show your program. 3.3 Design a function namely, sinwave(a,f,phi), such that it will show a sinusoid of amplitude A, frequency F and phase delay Phi. Note that no matter what the values of A, F,
Phi are, your program should be able to display only 3 cycles of the sinusoid. Show your program. 4. Fourier Series Fourier first shows that any periodic waveform can be synthesized by a sum of sinusoids that are harmonically related. To be specific, any continuous time periodic waveform can be represented by the Fourier series as follows: Or it can be written as follows: where are the Fourier series coefficients. For example, we can synthesize a square wave by using some harmonics of sinusoids. Let be a square wave as defined as follows: We know that it can be represented by Fourier series with the following coefficients To visualize how a square wave can be constructed by the summation of sinusoids, we can again make use of the MATLAB. The following program let us add together the first, 3 rd, 5 th and 7 th harmonics of a sinusoid with amplitude as defined above. You can see a waveform that is similar to a square wave is shown. A=2; f0 = 1000; phi = -pi/2; T0 = 1/f0;
tt = 0 : T0/400 : 4*T0; % All cosine functions are summed here xx = (4*A/pi/1)*cos(2*pi*f0*tt + phi)+... (4*A/pi/3)*cos(2*pi*3*f0*tt + phi)+... (4*A/pi/5)*cos(2*pi*5*f0*tt + phi)+... (4*A/pi/7)*cos(2*pi*7*f0*tt + phi); plot(tt,xx) axis([0,0.004,-4,4]) xlabel('time (sec)'); grid on 4.1 Try the program above by putting it into a M file. Show the result waveform 4.2 Design a function namely, squarewave(n), such that it will display a square wave constructed by the summation of N odd harmonics of a sinusoid. Show your program and the resulting waveform when N = 1, 2, 10, and 100. 4.3 Why we cannot achieve a perfect square wave when N = 100? 5. Tone generation Different tones can be generated by using sine wave. To generate a tone using sine wave, we may type the following according to the frequency we set. fs=8192; t=[0:1/fs:1]; y = sin(2*pi*freq*t); soundsc(y) 5.1 Consider setting freq to be 440*2^(1/12), what sound is that? 5.2 Consider the following different settings of frequencies, what sounds do they represent? 440*2^(n/12) where n is [3,5,6,7,10,12,13] 5.3 can you compose a simple song using the above note? Demonstrate your song before the end of the lab session.
6. References [1] T.A. Driscoll, Learning MATLAB, Philadelphia, PA : Society for Industrial and Applied Mathematics, c2009. [2] Using MATLAB, The Math Works Inc. Appendix : The Function of MATLAB A. General help [instruction] Displays help for [instruction] help Displays a list of available instructions dir ( or ls) Lists directory cd Change directory who (whos) Lists current variables format Set output (display) format, e.g. format compact ; (after command) Suppresses the display of result B. Matrix Row matrix : A = [1,2,3] or A = [1 2 3] Column matrix: A = [1:1:3] or A = [1, 2, 3] Rectangular matrix: A = [1 2 3 4: 5 6 7 8] To access an element: c = [A (row, column )]; e.g. A(2,3) To access an row: c = [A (row,:)]; e.g. A(1,:) = the first row of A To access an column: c = [A (:,column)]; Identity matrix: b = eye (n), where n is an integer Ones matrix: x = ones(m,n) Zeros matrix: y = zeros(m,n) length(x) Shows the length of vector x size(a) Shows the size of matrix A in (row, column) C. Special Variable and Constants Ans Most recent answer eps Floating point relative accuracy i, j Imaginary unit; e.g. c = 2 + 5i inf Infinity pi Nan Not a number D. Control Flow if {condition} else end
if {condition} elseif {condition} else end for j = 1:N end while {condition} end E. Mathematical Operators + Plus + Unary plus Minus Unary minus * Matrix multiply.* Element by element multiply \ Backslash or left matrix divide / Slash or right matrix divide.\ Left array divide./ Right array divide ^ power.^ Array power F. Functions abs Absolute value angle Phase angle cos cosine sin sine imag Complex imaginary part real Complex real part G. Useful Functions y = fft (x) or y = fft(x, n) FFT of x ; n is number of point [z, p, k] = buttap(n) butterworth analog prototype [num, den] = zp2tf (z, p, k) zero pole to transfer function [bz, az] = impinvar (num, den, fs) impulse invariant transformation [zd, pd, kd] = bilinear (z, p, k, fs ) bilinear transformation [h,w] = freqz (num, den, n) Z transform digital filter frequency response w = hamming (n) hamming window stem plot the signal in the impulse form