Linguistics 222b/622b

advertisement

Linguistics 582

Basics of Digital Signal Processing

Assignment 3: Moving Average (Feedforward) Filters

Reading:

 Hahn, Chap. 4. 104-112; Chap. 5; Chap. 10, 230-239.

 Steiglitz Chapter 4 (Sections 1-5; 7)

Exercises: Before doing the exercises, download Assignment3.zip from the web page and unzip.

(1) Create MATLAB scripts to generate a digital buzz using two different techniques:

(a) phasor technique (frequency domain): make_buzz_phasor.m

This script should generate a buzz signal that is the sum of the real part of a series of complex exponentials (phasors) that are successive multiples of some frequency (the fundamental frequency of the buzz). Make the buzz .5 seconds in duration.

First prompt the user for sampling rate ( srate ) and fundamental frequency ( f0 ).

Create a loop that generates a new phasor with the next highest frequency on each pass through the loop, and add it to the cumulative sum of the real part of the phasors. Begin with the fundamental frequency and end with the highest frequency that is less than the Nyquist frequency.

After the buzz has been generated, plot the first 200 samples of the time wave (in figure

(b)time domain technique: make_buzz_time.m

window (1)), and then call the spectrum function (in the Assignment3 folder) and plot the spectrum in Figure window (2).

This script should generate a signal that is a sequence of pulses at the fundamental period.

Again prompt for srate and f0.

Create a signal which contains all zeroes except for the first sample, and every srate/f0 samples after that. Try to do this without a loop. The use of logical vectors (see Hahn) will help here.

You might also find the following MATLAB function useful:

REM Remainder after division.

REM(x,y) is x - y.*fix(x./y) if y ~= 0. By convention, REM(x,0) is NaN.

The input x and y must be real arrays of the same size, or real scalars.

Compare the spectra of the buzz created by the two techniques. They should be the same, apart from a difference in overall amplitude scale.

(2) I have created a function to filter an input by averaging two successive points using the shift technique that we discussed in class ( filter_average2.m

). The code is shown below, and is included in Assignment3.zip. function Y = filter_average2 (X)

% filter_average2.m

% Louis Goldstein

% Jan 28 2005

% Create a new signal Y, by averaging two succesive points

% shift method

% Input argument: X input vector

% output argument: Y output vector

X_shifted = [0 X];

X_shifted(end) = [];

Y = .5*X + .5*X_shifted;

This is similar to the version I demonstrated in class. The input is shifted by one sample, then both the unshifted signal and the shifted signal are multiplied by .5, and the two resulting signals are added.

The only difference between this and the one I presented in class is that this one is a function. You can generate any signal you wish in the command window, and then call this function to filter it, and it returns the filtered signal.

For example, try the following. First generate a sinusoid using the make_tone

function, which is also included in Assignment3.zip, and is discussed in the Writing MATLAB Functions handout. Then filter it using filter_average2, as below:

>>lo = make_tone(10000,200); % generate a 200 Hz tone

>>out = filter_average2(lo); % filter using a moving average filter

Now plot the first 100 points of lo and of out . How much has the amplitude decreased as a result of filtering? Now do the same for a hi fiequency tone (close to the Nyquist frequency, say 4800 Hz). Does the amplitude drop more or less after filtering than does the l o tone? Why?

(3) In filter_average2.m

, both the unshifted and shifted signals are scaled by .5 before being added

(that is what makes it an averaging filter). However, the scaling values could be any numbers. Modify filter_average2.m so that the value of the scaling coefficients for the unshifted and shifted signals are input to the function in a vector, b . So now the beginning of the function, which you should call filter_2.m

will look like this: function Y = filter_2 (b, X)

% filter_2.m

% Louis Goldstein

% Jan 28 2005

% Create a new signal Y, by averaging two succesive points

% shift method

% Input arguments: X input vector

b filter coefficients

% output argument: Y output vector

X_shifted = [0 X];

X_shifted(end) = [];

Supply he rest of the code, multiplying the unshifted signal by b(1) and the shifted one by b(2), and adding them up.

With this function, test the following pairs of coefficients. In each case, use the hi and lo sinusoids you generated to test the resulting filter. Are they high pass, low pass, or neither (include the answer in your email).

(a) [ .5 -.5]

(b) [ 0 1]

(c) [-.5 -.5]

(d) [ 1 1]

(e) [.3 .7]

(3) Modify your function filter_2.m

so that vector of b coefficients can be of any length (N) , instead of just two. b(1) should be multiplied by the unshifted input, b(2) by the input shifted by one sample, b(3) by the input shifted by 2 samples, up to b(N) which is multiplied by the input shifted by N-1 samples. Call the new function filter_N.m

To test your filter, generate another test sinusoid, mid , (F = 2500 Hz). For the b vectors below, the filter can function as a band-pass or -reject filter. For each of the filters which of the frequencies ( hi, mid, lo ) are in pass regions, and which are in reject regions?

(a) [.5 0 .5]

(b) [.5 0 -.5]

(c) [-.5 0 -.5]

(d) [1 0 1]

Download