You are on page 1of 11

Communications II

Experiment II

COMMUNICATIONS II EXPERIMENT 2 Group member 1 Name: Number: Group member 2 Name: Number: 1. OBJECTIVES Designing Linear Filters in MATLAB.

2. BASIC INFORMATION Linear time invariant filters shape the spectrum of a signal. If the signal has too much energy in the low frequencies, a highpass filter can remove them. If the signal has too much high frequency noise, a lowpass filter can reject it. If a signal of interest resides only between f1 and f2, then a bandpass filter tuned to pass frequencies between f1 and f2 can remove out-ofband interference and noise. More generally, suppose that a signal has frequency bands in which the magnitude of the spectrum is lower than desired and other bands in which the magnitude is greater than desired. Then a linear filter can compensate by increasing or decreasing the magnitude. This experiment provides an overview of how to implement simple filters in Matlab. While the calculations of a linear filter are usually carried out in the time domain, filters are often specified in the frequency domain. Indeed, the words used to specify filters (such as lowpass, highpass, and bandpass) describe how the filter acts on the frequency content of its input. Figure 1, for instance, shows a noisy input entering three different filters. LPF: The frequency response of the LPF shows that it allows low frequencies (those below the cutoff frequency) to pass, while removing all frequencies above the cutoff. HPF: The HPF passes all the high frequencies and rejects those below its cutoff. BPF: The frequency response of the BPF is specified by two frequencies. It will remove all frequencies below some frequency and remove all frequencies above some other frequency, leaving only the region between. Figure 1 shows the action of ideal filters. In this experiment, we will see how close actual implementations are.

Communications II

Experiment II

Figure 1: A white signal containing all frequencies is passed through a lowpass filter (LPF) leaving only the low frequencies, a bandpass filter (BPF) leaving only the middle frequencies and a highpass filter (HPF) leaving only the high frequencies.

The Matlab code in filternoise.m shows that it is possible to create digital filters that reliably and accurately carry out these tasks.
% filternoise.m filter a noisy signal three ways time=3; % length of time Ts=1/10000; % time interval between samples x=randn(1,time/Ts); % generate noise signal figure(1),plotspec(x,Ts) % draw spectrum of input freqs=[0 0.2 0.21 1]; amps=[1 1 0 0]; b=firpm(100,freqs,amps); ylp=filter(b,1,x); figure(2),plotspec(ylp,Ts) freqs=[0 0.24 0.26 0.5 0.51 1]; amps=[0 0 1 1 0 0]; b=firpm(100,freqs,amps); ybp=filter(b,1,x); figure(3),plotspec(ybp,Ts) freqs=[0 0.74 0.76 1]; amps=[0 0 1 1]; b=firpm(100,freqs,amps); yhp=filter(b,1,x); figure(4),plotspec(yhp,Ts)

% specify the LP filter % do the filtering % plot the output spectrum

% BP filter % do the filtering % plot the output spectrum

% specify the HP filter % do the filtering % plot the output spectrum

The output of filternoise.m produces 4 figures. The time domain and frequency domain representations of the input signal and 3 output signals corresponding to LPF, BPF, and HPF. These plots are shown in Figures 2-5 below. Observe that the spectra at the output of the filters are close approximations to the ideals shown in Figure 1. There are some difierences, however. While the idealized spectra are completely at in the passband, the actual ones are rippled. While the idealized spectra completely reject the out-of-band frequencies, the actual ones have small (but nonzero) energy at all frequencies. 2

Communications II

Experiment II

Figure 2: Input signal


5

amplitude

-5

0.5

1.5 seconds

2.5

600

magnitude

400

200

0 -5000 -4000 -3000 -2000 -1000

0 1000 frequency

2000

3000

4000

5000

Figure 3: Output of LPF


2 1 amplitude 0 -1 -2

0.5

1.5 seconds

2.5

600

magnitude

400

200

0 -5000 -4000 -3000 -2000 -1000

0 1000 frequency

2000

3000

4000

5000

Communications II

Experiment II

Figure 4: Output of BPF

4 2 amplitude 0 -2 -4

0.5

1.5 seconds

2.5

600

magnitude

400

200

0 -5000 -4000 -3000 -2000 -1000

0 1000 frequency

2000

3000

4000

5000

Figure 5: Output of HPF


4 2 amplitude 0 -2 -4

0.5

1.5 seconds

2.5

500 400 magnitude 300 200 100 0 -5000 -4000 -3000 -2000 -1000 0 1000 frequency 2000 3000 4000 5000

Two new Matlab commands are used in filternoise.m. The firpm command specifies the contour of the filter as a line graph. For instance, typing plot ( [ 0 0.24 0.26 0.5 0.51 1] , [ 0 0 1 1 0 0 ] ) at the Matlab prompt draws a box that represents the action of the BPF designed in filternoise.m (over the positive frequencies). The frequencies are specified as percentages of fNYQ = 1/ (2Ts), which in this case is equal to 5000 Hz. (Note that Ts=1/10000 therefore 4

Communications II

Experiment II

fs=10000Hz and -5000 Hz through +5000Hz is shown in all of the frequency plots.) Thus the BPF in filternoise.m passes frequencies between 0.26x5000 Hz to 0.5x5000 Hz, and rejects all others. The filter command uses the output of firpm to carry out the filtering operation on the vector specified in its third argument. 3. EXPERIMENT Copy exp2 folder to your computer, start Matlab and change the directory of Matlab to exp2.

Type filternoise in Matlab command window and make sure you can observe Figure 2-5. Type edit filternoise and observe the m file in Matlab editor. Copy the part of filternoise.m related to LPF (first 11 lines) to a new file (File -> New -> m file) Save the new file as filternoiselpf.m in exp2 folder As shown in Figure 3 this lowpass filter passes frequencies below 1000 Hz. Replace freqs=[0 0.2 0.21 1]; with freqs=[0 0.1 0.11 1]; Save your results and run the matlab code by typing filternoiselpf in the Matlab command window. (You may want to type close all in matlab to close the previos figures before you produce new ones) Look at the output and explain the change you observe below also copy the output into Figure 6.

Explanation:

Code:

Copy the part of filternoise.m related to HPF to a new file (first 5 lines plus lines 19-23) Save the new file as filternoisehpf.m in exp2 folder Change the HPF filter to pass all frequencies above 500 Hz. Copy your code below also copy the output into Figure 7

Copy the part of filternoise.m related to BPF to a new file (first 5 lines plus lines 13-17) Save the new file as filternoisebpf.m in exp2 folder Change the BPF filter to pass all frequencies between 1500 Hz and 2500 Hz Copy the output into Figure 8 and the code below

Communications II

Experiment II

Code:

Code:

Band Reject filter: Save the file filternoisebpf.m as filternoisebrf.m in exp2 folder Design a filter to reject all frequencies between 1500 Hz and 2500 Hz Copy the output into Figure 9 and the code below

Code:

Once again open filternoiselpf.m. Design a filter to pass all frequencies below 2500 Hz. Copy the output into Figure 10 and the code below

Code:

Change the sampling rate to Ts=1/20000. Redesign the LPF filter to pass all frequencies below 2500 Hz. Copy the output into Figure 11 and the code below

In filternoise.m file the first part produces the input signal as time=3; % length of time Ts=1/10000; % time interval between samples x=randn(1,time/Ts); % generate noise signal figure(1),plotspec(x,Ts) % draw spectrum of input Now replace this part with where instead of a noise signal we have signal having three cosine signals with frequencies 800 Hz, 2000 Hz, and 4500 Hz. Save the file as filtercos.m time=3; % length of time Ts=1/10000; % time interval between samples t=0:Ts:time; x1=cos(2*pi*800*t); x2=cos(2*pi*2000*t); 6

Communications II

Experiment II

x3=cos(2*pi*4500*t); x=x1+0.5*x2+2*x3 figure(1),plotspec(x,Ts) % draw spectrum of input Copy the output to Figure 12-15 Explain which frequency components are present in each of the four signals and explain if these results are expected Explanation: Input signal: Output of LPF Output of BPF Output of HPF

Communications II

Experiment II

4. RECORDS Figure 6

Figure 7

Figure 8

Communications II

Experiment II

Figure 9

Figure 10

Figure 11

Communications II

Experiment II

Figure 12

Figure 13

Figure 14

10

Communications II

Experiment II

Figure 15

11

You might also like