Fifth&Sixth Procedure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Introduction to MATLAB

Sessions Five and Six Procedure

Sessions Five and Six Practical Procedure


Signal Processing in MATLAB
5-6.1 Purpose and Objectives
This document contains the theory and instructions for the hands-on laboratory work in the fifth and sixth sessions of this course. These sessions cover some useful signal processing functions, including Laplace transforms and Fourier transforms with example applications. Key objectives are: To learn how to use Laplace transforms to solve differential equations. To develop an understanding of Fourier series and Fourier transforms To become familiar with the applications of signal processing functions.

You should have a question sheet to accompany these sessions. If for any reason you havent got a copy of this, please ask a demonstrator.

5-6.2 Laplace Transforms


The Laplace transform of a function of time f(t) is given by the following integral:

L{ f (t )} = F ( s ) =

f (t ).e
0

st

dt

Laplace transforms have a wide range of applications. An important feature of the Laplace transform is that it transforms differential equations into algebraic equations, which should (in principle) be easier to solve. Laplace transforms can be computed in MATLAB with the laplace function, using symbolic computation. The default independent variable is t and the function returns a function of s. A list of Laplace transforms of common signals is often found in engineering textbooks. Here are some examples to compute in MATLAB: >> >> >> >> syms a t w laplace(t) laplace(exp(-a*t)) laplace(sin(w*t))

Following algebraic manipulation of Laplace transforms, it is often necessary to compute the inverse Laplace transform to convert the resulting expression back into the time domain. This is typically achieved through a partial fraction expansion and by referring to standard tables of transforms. In MATLAB, the ilaplace function can be used to compute the inverse Laplace transform, with the default independent variable s.

Dr P. D. Mitchell

1/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

The following examples show how the original time domain signals can be obtained from the Laplace transforms of the previous examples: >> >> >> >> syms s; ilaplace(1/s^2) ilaplace(1/(s+a)) ilaplace(w/(s^2+w^2))

Can you make sense of this last answer? It is not as complicated as it looks. Laplace transforms and inverse Laplace transforms can be stored in variables as you would expect and ezplot can be used to plot the expressions. Try the following: >> >> >> >> a = 2; w = 20; eq = ilaplace((s+a)/((s+a)^2 + w^2)) ezplot(eq);

The chosen scale is poor in this instance, but can be easily changed with the axis function: >> axis([0 5 -1 1]); In session two, the symbolic computing facilities of MATLAB were used to solve a differential equation, to give the time domain response of a simple RC charging circuit. We can now illustrate the use of Laplace transforms to solve the same problem. Unfortunately, the laplace function will not work directly on derivatives, so it is necessary to obtain the Laplace transform in algebraic form by hand and then use ilaplace to obtain the time domain solution. The previously considered RC charging circuit is depicted in figure 5-1 to illustrate this, with the components annotated with their Laplace transforms (obtained from any good textbook). The Laplace transform expression we are interested in is shown alongside the figure, derived through simple algebraic manipulation.

t=0 Vb = 12v

Vc =

Vc

1/SC

Vb SC . Vb = (1 + SRC )S +R S SC A B = + 1 + SRC S = RC.Vb Vb + 1 + SRC s

Figure 5-1: Simple RC Charging Circuit The time domain response can now be obtained by defining the symbols and applying ilaplace: >> syms Vb RC; >> eq = ilaplace(-(RC*Vb)/(1+s*RC) + (Vb/s))

Dr P. D. Mitchell

2/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

The result you obtain should be identical to the result obtained in session two in solving the differential equation with dsolve. Check this yourself. We can produce a plot of the time domain response with appropriate parameter values. You may remember that we produced a plot of this function in session three, using ezplot following a call to dsolve. In this instance, we need to recompute the inverse Laplace transform once the parameter values have been defined: >> RC = 10; Vb = 12; >> eq = ilaplace(-(RC*Vb)/(1+s*RC) + (Vb/s)) >> ezplot(eq, [0 50]); You should see the characteristic response of the circuit, charging from 0 to 12 Volts.

5-6.3 Fourier Series


Any periodic signal can be represented by an infinite series of harmonically related sines and cosines (known as a Fourier series) that sum together to make a periodic signal. Consider a periodic signal, f(t). The usual form of the Fourier series expression for Fourier synthesis is:

f (t ) = a0 +

(a .cos(n t ) + b .sin(n t ))
n 0 n 0 n =1

Where n represents the harmonic number, 0 is the (repetition) frequency of the periodic signal, a0, an, and bn are the Fourier coefficients. As an example, consider the Fourier series for a phase-shift keyed signal. Binary Phaseshift key (BPSK) modulation is often used to send binary information. Binary ones and zeros are distinguished by a phase reversal of the carrier wave. Figure 5-2 shows an example of a phase-shift keyed signal with 0 equal to 1 (corresponding to a period of 2), representing the transmission of 11001100.

Figure 5-2: Phase-Shift Keyed Signal

Dr P. D. Mitchell

3/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

This is an even function since f(t) = f(-t). This means that a harmonically related series of cosine waves exist, which sum to yield f(t). It can be shown that a0 and bn are both equal to zero and an is given by:

an =

16 (n 16)
2

where n is odd

To illustrate the synthesis of the waveform shown in figure 5-2 in MATLAB, lets create a vector of time values in the range -2 to 2 (as shown above), specify the values of n that we wish to include in the synthesis and calculate the Fourier coefficient values (an): >> t = [-2*pi:0.001:2*pi]; >> n = [1:2:49]; >> an = -16./((n.^2-16).*pi); In this example, we are only interested in odd values of n and calculate the first 25 coefficient values to limit the potentially infinite series to the first 25 harmonically related cosines. To start with, we can synthesise the first three cosine waves according to the Fourier series summation and plot the result. A for loop is handy for this purpose: >> >> >> >> >> ft = 0; for i = 1:1:3 ft = ft + an(i)*cos(n(i).*1.*t); end plot(t,ft);

The plot should resemble the signal shown in figure 5-2, even with the summation of only three cosine waves. We can now extend this to include the next 22 cosines: >> >> >> >> for i = 4:25 ft = ft + an(i)*cos(n(i)*1.*t); end plot(t,ft);

This should give you a much better representation that is hard to distinguish from figure 5-2. Can you spot any small difference? It is important to note the following. The cosines represent the spectral components of the phase-shift keyed signal. The process of Fourier synthesis takes you from the spectrum of a signal to the waveform. Given knowledge of the Fourier series coefficient values, we have used the Fourier series expression to synthesise the phase-shift keyed signal from its spectral components. Fourier analysis is the opposite process of going from a waveform to its spectrum. Calculation of the Fourier series coefficient values of a periodic signal (a0, an and bn) constitutes Fourier analysis (not covered fully here).

Dr P. D. Mitchell

4/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

5-6.4 Fourier Transforms


Remembering that Fourier series relate to periodic signals, the Fourier transform represents an extension of Fourier series for handling non-periodic signals. The usual form of the forward Fourier transform expression for Fourier analysis of a time domain signal is:

F ( ) =

f (t ).e

jt

dt

The usual form of the inverse Fourier transform expression for Fourier synthesis of a time domain signal is:

1 f (t ) = 2

F ( ).e

jt

The built-in MATLAB function fourier can be used to compute forward Fourier transforms, using symbolic computation. The default independent variable is x and the function returns a function of w. Here are a couple of examples: >> syms a t; >> fourier(dirac(t)) >> fourier(dirac(t-a)) These transforms can be found in any standard list of Fourier transforms, or you can calculate them by hand. The following example shows how a symbolic plot can be produced of a time domain signal, and corresponding frequency domain signal obtained with fourier: >> >> >> >> >> >> figure(1); clf; ezplot(exp(-abs(t))); ft = fourier(exp(-abs(t))) figure(2); ezplot(ft);

Inverse Fourier transforms can be obtained with the ifourier function. For example: >> ift = ifourier(ft) >> ezplot(ift); Notice that this inverse Fourier transform looks different to the original expression but the plot shows that it is the same as the original time domain signal, as it should be.

Dr P. D. Mitchell

5/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

MATLAB has a built-in function (fft) which can be used to determine the discrete Fourier transform of numerical vectors, computed with a Fast Fourier Transform (FFT) algorithm. A common use of this function is to find spectral components of signals buried within noisy time domain signals as demonstrated in the following example. Consider data sampled at 1000 Hz. Lets generate two sinusoidal signals of the form sin(2ft), one at a frequency of 50Hz and the other at 100Hz, combine them and plot the time domain signal: >> >> >> >> >> >> >> >> >> >> >> fs = 1000; t = 0:1/fs:1-1/fs; origsig = sin(2*pi*50*t)+sin(2*pi*100*t); figure(1); clf; subplot(2,1,1); plot(t, origsig); title('Original Signal'); xlabel('time (s)'); ylabel(Amplitude); axis([0 0.08 2 2]);

The structure of the combined signal can be seen in the figure, with the time scale covering four periods of the 50Hz signal and eight periods of the 100Hz signal. Now lets corrupt the signal with some zero-mean Gaussian noise and plot the resulting signal: >> >> >> >> >> >> >> noisysig = origsig + randn(size(t)); subplot(2,1,2); plot(t,noisysig); title('Signal Corrupted with Noise'); xlabel('time (s)'); ylabel(Amplitude); axis([0 0.08 2 2]);

The structure of the noisy signal is unclear and it would not be possible from this time domain signal to identify the underlying signals of interest, if we didnt already know what they were! Fourier analysis by means of the discrete Fourier transform can be used to identify the underlying spectral components of this signal. An n-point fast Fourier transform can be computed for a signal s by typing fft(s, n). If the signal has less than n points, it is padded with zeros. If it has more than n points, it is truncated. Lets try computing the fast Fourier transform of our noisy signal, following the same process as the example in the MATLAB help documentation. The first step is to set an appropriate FFT length for the signal: >> NFFT = 2^nextpow2(length(noisysig));

Dr P. D. Mitchell

6/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

The FFT can then be calculated: >> fftsig = fft(noisysig,NFFT)/length(noisysig); The single-sided amplitude spectrum can be obtained as follows: >> amp = 2*abs(fftsig(1:NFFT/2)); In order to plot this amplitude spectrum, an appropriately scaled vector of frequency values needs to be generated and plotted against the corresponding amplitude values: >> freq = fs/2*linspace(0,1,NFFT/2); >> figure(2) >> plot(freq,amp); The two frequency components should be easily visible on the plot, as large amplitude spikes. Zoom in to the figure and check the frequencies of the spikes. This example has focused on Fourier analysis of a noisy time domain signal, the process of converting a time domain signal into the Fourier domain and observing its spectral content. MATLAB also supports Fourier synthesis of discrete time signals with the inverse fast Fourier transform function (ifft). Fourier analysis has a wide range of useful applications. Examples include equalisation of audio signals, image processing and digital radio reception.

5-6.5 Exercises
Session 5-6 Exercise 1 A square wave is a periodic function and can therefore be represented by a Fourier series. Two successive periods of a square wave function are shown in Figure 5-3.

Figure 5-3: Square Wave Signal

Dr P. D. Mitchell

7/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

This is an odd function since f(t) = -f(-t). This means that a harmonically related series of sine waves exist, which sum to yield f(t). It can be shown that a0 and an are both equal to zero and bn is given by:
bn = 4 where n is odd n

Using the Fourier series expression for Fourier synthesis presented in section 5-6.3: A) Write a MATLAB function to synthesize the square wave shown in figure 5-3 (over two periods), based on a time time-domain Fourier series approximation. Your function should take the number of harmonics (sine waves), h, as an input and provide the time-domain vectors, t and f(t), as outputs. (Hint: The Fourier synthesis example of phase-shift keying should help. Be careful not to confuse the number of harmonics (h) with the harmonic number (n). To plot the summation of five harmonics, h = 5 and n is the first five harmonic numbers (odd) required for the summation, n = [1 3 5 7 9]). B) Write another function to call your first function with a specified number of harmonics and produce a plot showing the first harmonic on top of the square wave approximation, based on the specified number of harmonics. This function should take the number of harmonics (h) as an input. Use the function developed in part B) to produce plots of the square wave approximation resulting from 2, 10 and 25 harmonics.

C)

Session 5-6 Exercise 2 Analogue telephone lines use a Dual-Tone Multi-Frequency (DTMF) signalling standard between telephone handsets and switching centres, to identify which numbers have been dialled. When a number or symbol on the handset is pressed, a sinusoidal tone is generated comprising a distinct pair of frequencies (one low and one high). The dual-tone signals are then decoded at the switching centres to determine which key was pressed. Figure 5-4 shows the frequencies used for each key, which are based on the particular row and column in which the key is located.
1209Hz 1336Hz 1477Hz

697Hz

ABC

DEF

770Hz

GHI

JKL

MNO

852Hz

PQRS

TUV

WXYZ

0 # * Figure 5-4: Telephone keypad and corresponding dual tone frequencies

941Hz

Dr P. D. Mitchell

8/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

As an example of Fourier synthesis and analysis: A) Produce a normalised (maximum amplitude of one) DTMF signal for the number 5 in MATLAB. Your time domain signal vector (t) should be of 0.25s duration, with a 32,768 Hz sample rate. Illustrate the DTMF signal with two sub-plots, one showing the entire signal and one showing the signal over the first 10ms. (Hint: Remember that the general form of a sinusoidal signal is: y = sin(2 ft). You need to generate the two signals and then combine them). B) Write a MATLAB function to produce a DTMF signal sequence, based on a telephone number entered at the MATLAB prompt (e.g. 01904). Your function should generate 0.25s DTMF signals for each digit and display the first 10ms of each signal in separate figure windows. Your function should combine the signals together to form one continuous signal consisting of the 0.25s DTMF signals separated by 0.25s silence periods. The sample rate should be 32,768 Hz. Your function should play the resulting dialling sequence. (Hint: Use the code developed in part A) as a starting point. There are many different ways to write a suitable solution to this problem, but the key steps from an example solution are given below. You are free to find an alternative approach and may use fewer or more built-in functions in your solution. Marks will be given for each part, with maximum marks available for a complete solution, which automates the process as much as possible). Key steps: o Initialise variables o Read a telephone number from the MATLAB prompt (input may be useful). o Process each digit of the telephone number in turn (for may be useful). Determine the pair of frequencies corresponding to the digit (switch case may be useful). Generate the two sinusoids for the digit and combine them together to form the DTMF signal (sin and pi may be useful). Plot the first 10ms of the DTMF signal for the digit (plot and axis may be useful). Append a silence period on to the end of the DTFM signal (zeros may be useful). Append the new DTMF signal on to the end of the existing DTMF signal sequence

Dr P. D. Mitchell

9/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

o Play the complete DTMF signal sequence (sound may be useful). C) Run the MATLAB function written in B) with the York area code (01904). Keep a record of your plots. Listen to the sequence and determine whether it sounds like the same number being dialled on a conventional telephone. The dtmfsig.wav file contains a DTMF signal sequence of a telephone number being dialled. The sample rate is 44,100 Hz. Write a MATLAB script to extract the useful components of the signal (the frequencies of the successive pairs of sinusoidal signals) and determine the dialled number sequence. (Hint: The Discrete Fourier Transform (fft) and amplitude spectrum example should help you here. There are many different ways to write a suitable solution to this problem, but the key steps from an example solution are given below. You are free to find an alternative approach and may use fewer or more built-in functions in your solution. Marks will be given for each part, with maximum marks available for a complete solution, which automates the process as much as possible). Key steps: o Read the .wav file and store as a matrix (wavread may be useful). o Initialise variables o Plot and play the DTMF signal sequence (plot and sound may be useful). o Extract the DTMF signals for the individual digits from the DTMF signal sequence and store them in a suitable form (you may wish to use the plot to obtain the sample numbers required to extract appropriate sections of the signal). o Process each digit of the telephone number in turn (for may be useful). Generate the amplitude spectrum of the DTMF signal for the digit (fft and abs may be useful). Determine the two most significant frequency components based on the amplitude spectrum (sort may be useful here). Determine the closest DTMF signal frequencies to the observed frequency components (abs and min may be useful here). Determine which digit was dialled and store the new digit (conditional statements such as if..elseif may be useful here). o Print the complete telephone number in the MATLAB command window.

D)

Dr P. D. Mitchell

10/11

University of York

Introduction to MATLAB

Sessions Five and Six Procedure

5-6.6 Checkpoint
By the end of this session, you should have: + Familiarity with how to use MATLAB to solve differential equations with Laplace transforms. + An understanding of how to generate Fourier series and compute Fourier transforms + Knowledge of how MATLAB can be used to solve typical signal processing problems.

Dr P. D. Mitchell

11/11

University of York

You might also like