DSP3108 DSP Applications Labs Lab 1: Introduction to Matlab, sampling & digital frequency Objective: To become familiar with the Matlab simulation environment and to investigate sampled signals. M-files An M-file is a text file in Matlab containing a list of instructions of code. In general it is best to develop code within such a file. By developing the code this way, edits can be made easily and the resulting code tested for any number of times necessary. The M-file has an extension of .m. Create a test M-file called test.m as follows: Select File>New>Mfile from the menu system within Matlab. This opens up the note pad. Type in the following lines of code: A=10; B=20; C=A*B Now save this code using File>Save, then type in the name test.m. You can now run this program by simply typing test within the Matlab environment. Analogue Sinusoidal Signal The analogue sinusoidal signal (tone) is defined as: x(t ) A sin(at ) A sin(2 f at ) A is the amplitude of the tone, ω1 is the frequency in radians/sec and f1 is the frequency in Herz. Discrete Sinusoidal Signal In discrete form the tone is defined as: x (n ) A sin(a nT ), T 1 , f s sampling freq fs For a sampled signal time ‘t’ is replaced with t=nT where T is the sampling period. x(n) A sin(2 nf a / f s ) The factor 2 fa , the digital frequency fs It represents the angle that the analogue signal rotates through during one sampling period. x(n ) A sin(n ) Copy the code below into Matlab to investigate various digital frequencies. Change theta to π/32 and π/16 and plot the results. 1 DSP3108 DSP Applications Labs Tone of digital frequency pi/32 1 0.9 0.8 Amplitude (V) 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 5 10 15 Time (sec s) Tone of digital frequency pi/16 1 0.8 0.6 Amplitude (V) 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 5 10 15 Time (secs) A piece of Matlab code is included that plots an example of a single tone of amplitude A=10, and of frequency f1=1 kHz. Note that the period of this signal is 1 ms. % toneplot.m (c) A.Kelly. DIT Kevin St. % Plot of a sinusoidal signal A=10; fa=1000; fs=100000; n=0:1:200; t=1000*n/fs; q=2*pi*fa/fs; tone=A*sin(n*q); plot(t,tone); grid; xlabel 'Time ms' ylabel 'Amplitude (V)' 2 DSP3108 DSP Applications Labs title '1 kHz Tone of amplitude 10 V' 1 kHz Tone of amplitude 10 V 10 8 6 Amplitude (V) 4 2 0 -2 -4 -6 -8 -10 0 0.5 1 Time ms 1.5 2 (1) Modify the above program so that the signal being represented is: (i) s(t ) 3sin(2 f a t ) sin(2 (3 f a )t ) (ii) s(t ) cos(2 f a t ).*cos(2 (10 f a )t ) Do not use the same file name as any variable definition in your program. Plot the output signal in this case. Include your name/date on all plots. (2) Add a further modification to the last 2 programs that produces full-wave rectified signals and plot these 2 outputs. Save your work into a word document so that you can produce a report at a later stage without having to repeat the exercise. 3 DSP3108 DSP Applications Labs Previous version Lab 1: Introduction to Matlab, sampling & digital frequency Objective: To become familiar with the Matlab simulation environment and to investigate sampled signals. Procedure: Matlab (matrix laboratory) is an integrated technical computing environment that combines numeric computation, advanced graphics and visualisation, and a high level programming language. All input and output via Matlab is through matrices. Type the expressions that follow into the Matlab environment at the cursor ‘>>’ to see the effect of each instruction. All data manipulated in the matlab environment is in matrix form. For example: x=10 Defines a 1 by 1 matrix, x=[10] y=[1 2 3] Gives a 1 by 3 matrix y=[ 1 2 3] z=[1 2 4;4 5 6] results in a 2 by 3 matrix defined as: z= 1 4 2 5 4 6 Accessing Variables If we define v=[1 3 5 7] resulting in: Then assign v(2)=9 giving: v= 1 9 5 v=1 3 5 7 7 Each element in the matrix (array) v is accessed via v(1), v(2), v(3), v(4). Multiple dimensional arrays are also provided. The contents of the bracket is called the ‘index’ and note that it starts with 1 not 0. Matrix Manipulation Matlab can perform the standard matrix operations such as addition, subtraction, multiplication, transposition and invertion. For example X=magic(3) produces a 3 by 3 matrix whose rows, columns and diagonal elements all add up to the same value. 8 1 6 3 5 7 4 9 2 Addition: y=x+x 16 2 12 6 10 14 8 18 4 Multiplication: y=x*x 91 67 67 67 91 67 67 67 91 4 DSP3108 DSP Applications Labs The ‘dot’ Product Multiplication using the ‘dot’ product multiplies respective row and column elements by one another. This is very useful in signal processing as it allows you to multiply one array of data by another. This can provide techniques such a modulation, masking, windowing, etc. For example y=x.*x 64 1 36 9 25 49 16 81 4 Concatenation A=[1 2 3] B=[4 5 6] C=[A B] Produces: C= 1 2 3 4 5 6 Matrices A and B are joined to produce a single row matrix. The technique of joining sections together is useful when splitting up signals into time frames, processing each frame and then reconnecting the processed frames to produce the complete processed signal. The Column Array The previous section demonstrated the joining of 2 ‘row’ arrays. In certain circumstances, the data may be required to be in ‘column’ format. A=[1 2 3]’ B=[4 5 6]’ C=[A;B] Results in: C= 1 2 3 4 5 6 M-files An M-file is a text file in Matlab containing a list of instructions of code. In general it is best to develop code within such a file. By developing the code this way, edits can be made easily and the resulting code tested for any number of times necessary. The M-file has an extension of .m. Create a test M-file called test.m as follows: Select File>New>Mfile from the menu system within Matlab. This opens up the note pad. Type in the following lines of code: A=10; B=20; C=A*B Now save this code using File>Save, then type in the name test.m. You can now run this program by simply typing test within the Matlab environment. 5 DSP3108 DSP Applications Labs Analogue Sinusoidal Signal The analogue sinusoidal signal (tone) is defined as: x(t ) A sin(at ) A sin(2 f at ) A is the amplitude of the tone, ω1 is the frequency in radians/sec and f1 is the frequency in Herz. Discrete Sinusoidal Signal In discrete form the tone is defined as: x (n ) A sin(a nT ), T 1 , f s sampling freq fs For a sampled signal time ‘t’ is replaced with t=nT where T is the sampling period. x(n) A sin(2 nf a / f s ) The factor 2 fa , the digital frequency fs It represents the angle that the analogue signal rotates through during one sampling period. x(n ) A sin(n ) Copy the code below into Matlab to investigate various digital frequencies. Change theta to π/32 and π/16 and plot the results. 6 DSP3108 DSP Applications Labs Tone of digital frequency pi/32 1 0.9 0.8 Amplitude (V) 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 5 10 15 Time (sec s) Tone of digital frequency pi/16 1 0.8 0.6 Amplitude (V) 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 5 10 15 Time (secs) A piece of Matlab code is included that plots an example of a single tone of amplitude A=10, and of frequency f1=1 kHz. Note that the period of this signal is 1 ms. % toneplot.m (c) A.Kelly. DIT Kevin St. % Plot of a sinusoidal signal A=10; fa=1000; fs=100000; n=0:1:200; t=1000*n/fs; q=2*pi*fa/fs; tone=A*sin(n*q); plot(t,tone); grid; xlabel 'Time ms' ylabel 'Amplitude (V)' title '1 kHz Tone of amplitude 10 V' 7 DSP3108 DSP Applications Labs 1 kHz Tone of amplitude 10 V 10 8 6 Amplitude (V) 4 2 0 -2 -4 -6 -8 -10 0 0.5 1 Time ms 1.5 2 (1) Modify the above program so that the signal being represented is: (iii) s(t ) 3sin(2 f a t ) sin(2 (3 f a )t ) (iv) s(t ) cos(2 f a t ).*cos(2 (10 f a )t ) Do not use the same file name as any variable definition in your program. Plot the output signal in this case. (2) Add a further modification to the last 2 programs that produces full-wave rectified signals and plot these 2 outputs. (3) Determine the average value, the peak value and the rms value of each of the signals defined previously. (4) Create an addition to the program that plots a hamming windowed spectrum of the signals defined in part (1).. Insure that the frequency axis is scaled correctly in Hz or kHz. Note that the following commands can be used in your code: delfreq=fs/N; % Frequency resolution freqval=(1:N/2); % Symmetrical about fs/2 freqval=freqval*delfreq; % Frequency axis spect=abs(fft(hamming(N)'.*tone)); % abs produces magnitude spectrum from fft spect=spect(1:N/2); Save your work into a word document so that you can produce a report at a later stage without having to repeat the exercise. 8