12
Signal Processing First Lab 10: Octave Band Filtering Pre-Lab and Warm-Up: You should read at least the Pre-Lab and Warm-up sections of this lab assignment and go over all exercises in the Pre-Lab section before going to your assigned lab session. Verification: The Warm-up section of each lab must be completed during your assigned Lab time and the steps marked Instructor Verification must also be signed off during the lab time. One of the laboratory instructors must verify the appropriate steps by signing on the Instructor Verification line. When you have completed a step that requires verification, simply demonstrate the step to the TA or instructor. Turn in the completed verification sheet to your TA when you leave the lab. Lab Report: It is only necessary to turn in a report on Sections 4 and 5 with graphs and explanations. You are asked to label the axes of your plots and include a title for every plot. In order to keep track of plots, include your plot inlined within your report. If you are unsure about what is expected, ask the TA who will grade your report. 1 Introduction This lab introduces a practical application where we attempt to extract information from sinusoidal signals— in this case, piano notes. Bandpass FIR filters can be used to extract the information encoded in the wave- forms. The goal of this lab is to design and implement several bandpass FIR filters in MATLAB, and use the filtered outputs to determine automatically which note is being played. However, since there are 88 keys on the piano, we will only require the system to figure out which octave the note is in, not the exact note. In the experiments of this lab, you will use firfilt(), or conv(), to implement filters and freqz() to obtain the filter’s frequency response. 1 As a result, you should learn how to characterize a filter by knowing how it reacts to different frequency components in the input. 1.1 Frequency Response of FIR Filters The output or response of a filter for a complex sinusoid input, e j ˆ ωn , depends on the frequency, ˆ ω. Often a filter is described solely by how it affects different frequencies—this is called the frequency response. The frequency response of a general FIR linear time-invariant system is H (e j ˆ ω )= M k=0 b k e -j ˆ ωk (1) MATLAB has a built-in function for computing the frequency response of a discrete-time LTI system. The following MATLAB statements show how to use freqz to compute and plot the magnitude (absolute value) of the frequency response of an L-point averaging system 2 as a function of ˆ ω in the range -π ˆ ω π: 1 If you are working at home and do not have the function freqz.m, there is a substitute available in the SP First toolbox called freekz.m. 2 The filter length L is equal to M +1. McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. c 2003 Pearson Education, Inc. 1

Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

Embed Size (px)

Citation preview

Page 1: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

Signal Processing FirstLab 10: Octave Band Filtering

Pre-Lab and Warm-Up: You should read at least the Pre-Lab and Warm-up sections of this lab assignmentand go over all exercises in the Pre-Lab section before going to your assigned lab session.

Verification: The Warm-up section of each lab must be completedduring your assigned Lab time andthe steps markedInstructor Verificationmust also be signed offduring the lab time. One of the laboratoryinstructors must verify the appropriate steps by signing on theInstructor Verification line. When you havecompleted a step that requires verification, simply demonstrate the step to the TA or instructor. Turn in thecompleted verification sheet to your TA when you leave the lab.

Lab Report: It is only necessary to turn in a report on Sections 4 and 5 with graphs and explanations. Youare asked tolabel the axes of your plots and include a title for every plot. In order to keep track of plots,include your plotinlined within your report. If you are unsure about what is expected, ask the TA who willgrade your report.

1 Introduction

This lab introduces a practical application where we attempt to extract information from sinusoidal signals—in this case, piano notes. Bandpass FIR filters can be used to extract the information encoded in the wave-forms. The goal of this lab is to design and implement several bandpass FIR filters in MATLAB , and use thefiltered outputs to determine automatically which note is being played. However, since there are 88 keys onthe piano, we will only require the system to figure out which octave the note is in, not the exact note. Inthe experiments of this lab, you will usefirfilt() , or conv() , to implement filters andfreqz() toobtain the filter’s frequency response.1 As a result, you should learn how to characterize a filter by knowinghow it reacts to different frequency components in the input.

1.1 Frequency Response of FIR Filters

The output orresponseof a filter for a complex sinusoid input,ejωn, depends on the frequency,ω. Often afilter is described solely by how it affects different frequencies—this is called thefrequency response. Thefrequency response of a general FIR linear time-invariant system is

H(ejω) =M∑

k=0

bke−jωk (1)

MATLAB has a built-in function for computing the frequency response of a discrete-time LTI system. Thefollowing MATLAB statements show how to usefreqz to compute and plot the magnitude (absolute value)of the frequency response of anL-point averaging system2 as a function ofω in the range−π ≤ ω ≤ π:

1If you are working at home and do not have the functionfreqz.m, there is a substitute available in the SP First toolboxcalledfreekz.m .

2The filter lengthL is equal toM + 1.

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

1

Page 2: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

bb = ones(1,L)/L; %-- Filter Coefficientsww = -pi:(pi/100):pi; %-- omega hat frequency axisHH = freqz(bb, 1, ww); %<--freekz.m is an alternativesubplot(2,1,1);plot(ww, abs(HH))subplot(2,1,2);plot(ww, angle(HH))xlabel(’Normalized Radian Frequency’)

We will always use capitalHHfor the frequency response. For FIR filters, the second argument offreqz(, 1, ) must always be equal to1. The frequency vectorwwshould cover the interval−π ≤ ω ≤ π forω, and its spacing must be fine enough to give a smooth curve forH(ejω).

2 Background and Pre-Lab

2.1 Piano Notes

A piano keyboard consists of 88 keys grouped into octaves. Each octave contains 12 notes, the notes in oneoctave being twice the frequency of the notes in the next lower octave.

Middle-C A-440

C3 D3 E3 F3 G3 A3 B3 C4 D4 E4 F4 G4 A4 B4 C5 D5 E5 F5 G5 A5 B540 42 44 45 47 49 51 52 54 56 57 59 61 6339373533323028

41 43

OCTAVE

Figure 1: Layout of a piano keyboard. Key numbers are shaded. The notationC4 means the C-key in thefourth octave.

Frequencies of the notes are defined by setting the frequency of one note and referring all other frequen-cies to that note. The reference note is the A above middle-C, which is usually called A-440 (orA4) becauseits frequency is 440 Hz. Each octave contains 12 notes (5 black keys and 7 white) and the ratio between thefrequencies of the notes is constant between successive notes. As a result, this ratio must be21/12. Sincemiddle C is 9 keys below A-440, its frequency is approximately 261 Hz. Consult Chapter 3 for more details.

If we want to produce a system capable of writing music directly from a recorded signalx(t), we needto analyze the frequency content of the signal. One way to do this analysis is to use a set of bandpass FIRfilters, each one having its passband designed for one note on the keyboard. This would require a very largenumber of filters. Another way to do the analysis would be to use a two stage approach. First, we woulduse a set of bandpass FIR filters where each passband would pass the frequencies in one octave. Then these“octave filters” would be followed by more precise bandpass filters (BPFs) that would determine which keyinside the octave is being played. The work in this lab will be to produce a working set of “octave filters.”

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

2

Page 3: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

3 Warm-up: Bandpass Filtering

3.1 Create a Bandpass Filter (BPF)

There are many ways to get the filter coefficients for a bandpass filter. One easy choice is to define theimpulse response of anL-point FIR filter as:

h[n] =2L

cos (ωcn), 0 ≤ n < L (2)

whereL is the filter length, andωc is the center frequency that defines the frequency location of the passband.For example, we would pickωc = 0.2π if we want the peak of the filter’s passband to be centered at0.2π.The bandwidth of the bandpass filter is controlled byL; the larger the value ofL, the narrower the bandwidth.This particular filter is also discussed in the section on useful filters in Chapter 7.

(a) Generate the impulse response of a length-25 bandpass filter withωc = 0.2π, and ploth[n] with astem plot.

(b) Compute the frequency response of the length-25 BPF from the previous part and plot its magnitudeand phase versusω over the range−2π ≤ ω ≤ 2π.

(c) Use the magnitude response plot to describe the passband of the BPF. For example, measure thebandwidth at a convenient point such as 50% of the peak.

Instructor Verification (separate page)

3.2 Overlay Plotting

Sometimes it is convenient to overlay information onto an existing MATLAB plot. The MATLAB commandhold on will inhibit the figure erase that is usually done just before a new plot. Demonstrate that youcan do an overlay by following these instructions:

(a) Plot the magnitude response of the 25-point BPF filter defined in (2). For this plot, it is sufficient touse a horizontal frequency axis that extends from−π to +π.

(b) Use thestem function to place vertical markers at several points on the frequency response at fre-quencies{0, ±0.2π, ±0.5π }.

hold on, stem(pi*[-0.5,-0.2,0,0.2,0.5],0.9*ones(1,5),’r.’), hold off

3.3 Signal Concatenation

In a previous lab, a very long music signal was created by joining together many sinusoids. When twosignals are played one after the other, the composite signal is created by the operation ofconcatenation.InMATLAB , this can be done by making each signal a row vector, and then using the matrix building notationas follows:

xx = [ xx, xxnew ];

wherexxnew is the sub-signal being appended. The length of the new signal is equal to the sum of thelengths of the two signalsxx andxxnew . A third signal could be added later on by concatenating it toxx .

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

3

Page 4: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

3.3.1 Comment on Efficiency

In MATLAB the concatenation method,xx = [ xx, xxnew ] , would append the signal vectorxxnewto the existing signalxx . However, this becomes aninefficientprocedure if the signal length gets to be verylarge. The reason is that MATLAB must re-allocate the memory space forxx every time a new sub-signalis appended via concatenation. If the lengthxx were being extended from 400,000 to 401,000, then a cleansection of memory consisting of 401,000 elements would have to be allocated followed by a copy of theexisting 400,000 signal elements and finally the append would be done. This is very inefficient for longsignals, but would not be noticed for short signals.

An alternative is to pre-allocate storage for the complete signal vector, but this can only be done if thefinal length is known ahead of time.

3.4 Filtering a Signal

Use the filter from a previous section (Sec. 3.1) to process an input signal composed of several sinusoids:

x[n] =

cos(0.5πn) 0 ≤ n < 2002 200 ≤ n < 4000.5 cos(0.2πn) 400 ≤ n < 600

(a) Generatex[n] in a vector calledxx .

(b) Filter xx to obtainyy .

(c) Makestem plots ofxx andyy .

(d) Use the frequency response to validate that the output signal has the correct magnitude and phase ineach of the three regions where the input has different frequencies.

(e) Comment: observe thetransienteffect at the transitions (n = 200 andn = 400). This is due tothe start-up of the FIR filter as it encounters a new sinusoid. How long does the transient last (insamples)?

Instructor Verification (separate page)

4 Lab Exercises: Bandpass Filter Design

Section 4.1 should not be included in the lab report.It is provided as an extension of the Warm-up.Section 4.2 must be included.

4.1 Simple Bandpass Filter Design

The L-point averaging filter is a lowpass filter. Its passband width is controlled byL, being inverselyproportional toL. It is also possible to create a filter whose passband is centered around some frequencyother than zero. One simple way to do this is to define the impulse response of anL-point FIR as:

h[n] =2L

cos (ωcn), 0 ≤ n < L

whereL is the filter length, andωc is the center frequency that defines the frequency location of the centerof the passband.

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

4

Page 5: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

(a) Generate a bandpass filter that will pass a frequency component atω = 0.4π. Make the filter length(L) equal to 40. Make a plot of the frequency response magnitude and phase.

(b) The passbandof the BPF filter is defined by the region of the frequency response where|H(ejω)|is close to its maximum value of one. Typically, the passband width is defined as the length of thefrequency region where|H(ejω)| is greater than some level such as 0.5, or0.707 = 1/

√2. Note:

you can use MATLAB ’s find function to locate those frequencies where the magnitude satisfies|H(ejω)| ≥ 0.5 (similar to Fig. 2). Use the plot of the frequency response for the length-40 bandpassfilter from part (a), and determine the passband width using the 0.5 level to define the pass band.

(c) Make two other plots of BPFs forL = 20 andL = 80 with the sameωc; and measure the passbandwidth for both. Then explain how the width of the passband is related to filter lengthL, i.e., whathappens whenL is doubled or halved.

0 0.5 1 1.5 2 2.5 30

0.2

0.4

0.6

0.8

1

Frequency (radians)

Mag

nitu

de

BANDPASS FILTER (centered at 0.4π)

PASSBAND

STOPBAND STOPBAND

0.4π

Figure 2: The frequency response of an FIR bandpass is shown with its passband and stopband regions. Inthis case, the passband is defined to be the region where|H(ejω)| is greater than 0.5; and the stopband isthe region where the magnitude is less than 0.1.

4.2 A Better BPF

It is possible to get better performance in the frequency response by modifying the filter coefficients slightly.One easy way is to use a “Hamming window.” In this case, the impulse response for a length-L bandpassfilter would be given as:

h[n] = (0.54− 0.46 cos(2πn/(L−1))) cos(ωc(n− (L−1)/2)) for n = 0, 1, 2, . . . L−1 (3)

whereωc is the desired center frequency for the BPF. The first term is the Hamming window. As before, thefilter lengthL determines the passband width, although the Hamming BPF tends to be a little wider than theBPF in the previous section. The big advantage of the Hamming BPF is that its stopband has ripples thatare very small (usually less than 0.01). Use zooming to figure out the height of the ripples in the stopbandwhen you plot the frequency response (below).

(a) Generate a Hamming bandpass filter that will pass a frequency component atω = 0.25π. Makethe filter length(L) equal to 41. Make a plot of the frequency response magnitude and phase.Measure the response of the filter (magnitude and phase) at the following frequencies of interest:ω = {0, 0.1π, 0.25π, 0.4π, 0.5π, 0.75π }. Summarize the values in a table.

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

5

Page 6: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

Hint: use MATLAB ’s freqz() function to calculate these values, or thefind() function to extractthis information from the vector that produce the plot.

(b) Thepassbandof the BPF filter is defined by the region of the frequency response where|H(ejω)| isclose to its maximum value of one. In this case, the passband width is defined as the length of thefrequency region where|H(ejω)| is greater than50% of the peak magnitude value.Thestopbandofthe BPF filter is defined by the region of the frequency response where|H(ejω)| is close to zero.

Use the plot of the frequency response for the length-41 bandpass filter from part (a), and determinethe passband width using the 50% level to define the pass band. Make two other plots of BPFs forL = 21 andL = 81, and measure the passband width in both. Then explain how the width of thepassband is related to filter lengthL, i.e., what happens whenL is (approximately) doubled or halved.

(c) If the input signal to the length-41 FIR BPF is:

x[n] = 2 + 2 cos(0.1πn + π/3) + cos(0.25πn− π/3)

Determine (by hand) the formula for the output signal. (Hint: use the magnitude and phase measure-ments from part (a).) Comment on the relative amplitudes of the three signal components in the outputsignal, observing whether or not they were in the passband or stopband of the filter.

(d) Use the frequency response (and passband width) of the length-41 bandpass filter to explain how thefilter is able to pass the components atω = ±0.25π, while reducing or rejecting others.

5 Lab Exercises: Piano Note Decoding

It is possible to decode piano signals into octaves using a simple FIR filter bank. The filter bank in Fig. 3consists of seven bandpass filters which each pass only one of the seven possible octaves. The input signalfor all the filters is the same.

bbb-

-

-

-

-

-

-

-

-

-

-

Octave #3

Octave #2

Octave #1

Octave #6

Octave #7

y1[n]

y2[n]

y3[n]x[n]

y6[n]

y7[n]

Figure 3: Filter bank consisting of bandpass filters which pass frequencies corresponding to the seven oc-taves on a piano. (Each octave contains 12 keys, but we will ignore the four extra keys outside the sevencomplete octaves.)

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

6

Page 7: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

The octave filtering system needs two pieces: a set of bandpass filters (BPFs) to isolate individualfrequency bands (i.e., the octaves), and a detector to determine whether or not a given octave is active. Thedetector must “score” each BPF output and determine whether or not the octave contains one or more notes.In a practical system where noise and interference are also present, this scoring process is a crucial part ofthe system design, but we will only work with noise-free signals to illustrate the basic functionality of thesystem.

To make the whole system work, you will have to write at least three function M-files:

1. A filter design function to produce the filter coefficients for the BPFs in Fig. 3.

2. A filtering function that will filter the input signal through each channel.

3. A scoring function that will evaluate the output of each channel for “activity.”

The following sections discuss how to create or complete these functions.

5.1 Piano Octaves

The bandpass filter specs are determined by the frequencies of different octaves on the piano. The standardnotation is to start an octave at the “C” key. For example, octave #4 starts at “middle-C” which is key #40and extends up to key #51; the fifth octave starts at key #52 and extends to key #63, and so on. See Fig. 1for the layout of a piano keyboard.

In this implementation, we want to design filters that will isolate five different octaves: octaves #2through #6. Octave #2 starts with key #16, octave #3 starts at key #28, and octave #6 starts at key #64 (thelast key in octave #6 is key #75).

In order to design the BPFs, we need the upper and lower frequencies for each octave inω. We canconvert key number to frequency in hertz, and then use the sampling frequency to convert analog frequencyto ω. For this lab, assume that the sampling frequency isfs = 8000 Hz.

Make a table defining the five BPFs that you must design.

(a) For each octave, determine the lower and upper frequencies which will become the lower and upperedges of the passband of the BPF. Give these numbers in hertz and also inω.

(b) Then compute the center frequencies of the BPFs as the midpoint between the lower and upper bandedges of the filter.

Save this information for use in the Filter Design Section (next).

5.2 Bandpass Filter Bank Design

The FIR filters that will be used in the filter bank (Fig. 3) should be constructed according to the Hammingimpulse responses of (3). These “Hamming” BPFs require two parameters: the lengthL and the centerfrequencyωc. Use the previous table of piano octave frequencies to define the passbands. Furthermore, thefilters should be scaled so that their maximum magnitude at the center frequency of the passband is equal toone. This can be done by introducing a third parameterβ that will scale all the filter coefficients.

h[n] = β (0.54− 0.46 cos(2πn/(L−1))) cos(ωc(n− (L−1)/2)) for n = 0, 1, 2, . . . L−1 (4)

The constantβ gives flexibility for scaling the filter’s gain to meet a constraint such as making the maximumvalue of the frequency response equal to one. The bandwidth of the bandpass filter is controlled byL; thelarger the value ofL, the narrower the bandwidth.

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

7

Page 8: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

(a) Devise a strategy for picking the constantβ so that the maximum value of the frequency response willbe equal to one. Write the one or two lines of MATLAB code that will do this scaling operation ingeneral. There are two approaches here:

(a) Mathematical:derive a formula forβ from the formula for the frequency response of the BPF.Then use MATLAB to evaluate this closed-form expression forβ. In this case, such analysis isnearly impossible.

(b) Numerical: use MATLAB ’s max function to measure the peak value of the unscaled frequencyresponse, and then have MATLAB computeβ to scale the peak to be one.

(b) You must design five separate bandpass filters for the filter bank system. Each filter has a differentlength and a different center frequency. The lengths have to be different because the bandwidths ofthe octaves are different. In fact, the bandwidth of each octave is twice that of the next lower octave.

For each filter, determine the lengthL that is required to get the correct bandwidth. Use the bandedgesdetermine in Section 5.1. This filter design process will be trial-and-error, so each time you changeLyou will have to make a frequency response plot (magnitude only) to see if the filter is correct.

(c) Generate the five (scaled) bandpass filters. Plot the magnitude of the frequency responses all togetheron one plot (the range0 ≤ ω ≤ π is sufficient because|H(ejω)| is symmetric). Indicate the locationsof each of the center frequencies of the five octaves on this plot and illustrate that the passbands coverthe separate octaves. Hint: use thehold command and markers as you did in the warm-up.

(d) As help for the previous parts, here are some comments: Thepassbandof the BPF filter is defined bythe region ofω where|H(ejω)| is close to one. In this lab, the passband width is defined as the lengthof the frequency region where|H(ejω)| is greater than0.5.

Filter Design Specifications:For each octave, chooseL so that the entire octave of frequencies lieswithin the passband of the BPF.

Thestopbandof the BPF filter is defined by the region ofω where|H(ejω)| is close to zero. In thiscase, we can define the stopband as the region where|H(ejω)| is less than 0.01 because the Hammingfilters have excellent stopbands. Notice, however, that the stopbands do not eliminate all the otheroctaves because the stopband does not start where the passband ends. There is a small “transitionregion” between the pass and stop bands where the frequency response is small, but not as small as inthe stopband.

Use thezoom on command to examine the frequency response over the frequency domain wherethe octaves lie. Comment on the selectivity of the bandpass filters, i.e., use the frequency response(passbands and stopbands) to explain how the filter passes one octave while rejecting the others. Arethe filter’s passbands narrow enough so that only one octave lies in the passband and the others are inthe stopband?

5.3 Piano Octave Decoding

There are several steps to decoding a piano signal to figure out which note is being played:

1. Filter the individual segments to extract the possible frequency components using carefully designedBPFs.

2. Determine a set of time segments over which to examine the output signals. In general, this could behard to implement so we will restrict the notes to occur at regular intervals based on the timing of thesong.

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

8

Page 9: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

3. Determine which frequency components are present in each time segment by measuring the size ofthe output signal from all of the bandpass filters. This tells you which octaves are active.

Here is how the system should work:When the input to the filter bank is a piano signal, the outputs froma few of the bandpass filters (BPFs) should be larger than the rest. If we can detect (or measure) the largeoutputs, then we know which octaves contain notes. These octaves are then used to label the notes partially.

A good measure of the output levels is thepeak valueof the filter outputs, because when the BPF isworking properly it should pass only the sinusoidal signals within one octave and the peak value wouldrepresent the combined amplitude of those sinusoids. If there is only one sinusoid within the octave then thepeak value is exactly equal to the amplitude of the one sinusoid present.

(a) In order to test your design so far, generate the following signal composed of several sinusoids:

x(t) =

cos(2π(220)t) 0 ≤ t < 0.25 secs.cos(2π(880)t) 0.3 ≤ t < 0.55 secs.cos(2π(440)t) + cos(2π(1760)t) 0.6 ≤ t < 0.85 secs.

Use a sampling rate offs = 8000 Hz, and generatex(t) in a vector calledxx .

(b) Filter xx through the five filters of the filter bank

(c) Make plots of the five outputs all together in one figure (usesubplot(5,1,n) ).

(d) Use the frequency responses to validate that the output signals have the correct magnitude and phasein each of the three regions where the input has different frequencies. You must determine whichoctave the signals are in.

(e) Comment: observe thetransienteffect at the transitions. This is due to the start-up of each FIR filteras it encounters a new sinusoid. How long does the transient last (in seconds)? Is it different for eachfilter in the filter bank?

5.4 Scoring for Activity

The final objective is detecting which octaves contain notes. In order toautomatethis detection process, weneed ascorefunction that rates each octave for possible activity.

(a) Complete theoctavescore function based on the skeleton given in Fig. 4. The input signalxxto theoctavescore function must be the input signal containing all the notes. Rather than tryingto detect the beginning and end of individual notes, we will have the score function calculate a scoreevery 50 milliseconds.

(b) Use the following rule for scoring: the score for thei-th channel equalsmaxn|yi[n]|, where the maxi-

mum is taken over a 50 millisecond interval. The signalyi[n] is the output of thei-th BPF. Since scoremust be computed every 50 milliseconds, each channel has a score vector with 20 scores per second.

(c) Note: After filtering and prior to scoring, adjust the output signal for the delay through the filter. Sinceeach filter has a different length, the delays are slightly different. For the Hamming BPFs, the delayis (L−1)/2. This is aminor issue, but it might cause some misalignment of the output scores.

(d) Finally, use all the score vectors together to implement an automatic detection system, based onthe fact that all filter passbands are above 50%. If all the input signals have an amplitude of one,

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

9

Page 10: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

function sc = octavescore(xx, hh, fs)%OCTAVESCORE% usage: sc = octavescore(xx, hh, fs)% returns a score based on the max amplitude of the filtered output% xx = input signal containing musical notes% hh = impulse response of ONE bandpass filter% fs = sampling rate%% The signal detection is done by filtering xx with a length-L% BPF, hh, and then finding the maximum amplitude of the output% within 50 millisecond segments.% The score is a vector containing the maximum amplitudes% of all the segments.

Figure 4: Skeleton of theoctavescore.m function. Complete this function with additional lines of code.

then comparing the scores to a threshold of 0.5 would detect the presence of a signal. This is a bigassumption on the inputs, but it will be true in the test data (below).

Comparing the scores to a threshold will give an output that is either zero (score less than threshold)or one (score greater than threshold). So you will end up with a5 × N matrix consisting of onesand zeros, whereN is the number of 50-millisecond intervals. Use your experience with plotting theoutput signals in the previous section to judge how well your automatic system is working, i.e., arethe ones in the correct locations and correct octaves?

5.4.1 Testing

Once you get your system working you could test it in three ways (only #1 needs to be included with the labreport):

1. Use the test signal provided with the lab in in ’MATLAB Files’ link which contains the MAT filecalled labtest.mat. It contains a signal sampled at 8000 samples/sec whose duration is 3.77

nkhtCD-ROM

labtest.matseconds. The signal has either 0, 1 or 2 sinusoidal notes present at one instant of time. The amplitudeof each sinusoid is one. You should run your system to determine which octaves are active in each50-millisecond time slice. The MAT file also contains a matrix callednotes which has the correctnotes in the input (as key numbers). The durations vary between 0.16 and 0.32 secs.

2. Generate a music signal that is a progression of notes starting at key #16, ending at key #87, andtaking every other note, or every third note. To keep the overall signal reasonably short, make eachnote about 0.2 secs. long, and make the amplitude of every sinusoid equal to one. This test signal isuseful because you know the correct answer.

3. Generate your own musical tones, as you did in Lab 04. Make sure to usefs = 8000 Hz. In this case,you will have several notes playing simultaneously, so the system will have a hard time making 100%correct decisions.

When you run your system, there might be errors some of the time, especially when a note frequency is nearthe boundary between two octaves. Summarize the behaviour of your test cases above, and comment on howwell your system works. For example, what is the percentage of errors (i.e., incorrect octave designations)?What happens if two simultaneous notes are in the same octave?

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

10

Page 11: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

5.4.2 Demonstration

When you turn in your lab report, you must demonstrate to the Lab TA that your system works correctly.We will have a “mystery” signal similar to the signal inlabtest.mat .

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

11

Page 12: Octave Band Filtering - DIKUjda/biosignal/lab10.pdfLab 10: Octave Band Filtering ... completed verification sheet to your TA when you leave the lab. Lab Report: ... In a previous

Lab 10INSTRUCTOR VERIFICATION PAGE

For each verification, be prepared to explain your answer and respond to other related questionsthat the lab TA’s or professors might ask. Turn this page in at the end of your lab period.

Name: Date of Lab:

Part 3.1: Create a bandpass filter; plot the magnitude response, and then determine the width of the passband at 50% of the peak.

Verified: Date/Time:

Part 3.4: Filter the three-sinusoid signal through the BPF, and explain the amplitudes of the output signal inthe three sections.

Verified: Date/Time:

McClellan, Schafer, and Yoder,Signal Processing First, ISBN 0-13-065562-7.Prentice Hall, Upper Saddle River, NJ 07458.c©2003 Pearson Education, Inc.

12