How to Use SPTool

7.3 How to Use SPTool

SPTool is an interactive GUI for digital signal processing that can be used to - Analyze signals (Signal Browser)

- Design filters (FDATool: Filter Design & Analysis Tool) - Analyze (view) filters (FVTool: Filter Visualization Tool) - Filter signals - Analyze signal spectra (Spectrum Viewer)

Signals, filters, and spectra can be brought from the MATLAB workspace into the SPTool workspace using File/Import. Signals, filters, and spectra created/modified in or imported into the SPTool workspace can be saved (as MATLAB structures) using File/Export.

Figure 7.27(a) shows the SPTool window opened by typing ‘sptool’ into the MATLAB command window where you can access the three GUIs, i.e., Signal

7.3 How to Use SPTool 351

Fig. 7.27 SPTool window, Signal Browser window, and Spectrum Viewer window

352 7 Analog and Digital Filters Browser (Fig. 7.27(b)), Filter Visualization Tool (Fig. 7.20(a)), and Spectrum Viewer

(Fig. 7.27(c)) by selecting a signal, filter, or spectrum and clicking the appropriate View button. Note that if you start FVTool by clicking the SPTool Filter View button, FVTool is linked to SPTool so that any changes made in SPTool are immediately reflected in FVTool where the FVTool title bar includes “SPTool” to indicate the link. (Every time you click the Filter View button, a new, linked FVTool starts, which allows you to view multiple analyses simultaneously. Any parameter except the sampling frequency can be changed in a linked FVTool. The sampling frequency can be changed through the Sampling Frequency dialog box opened by selecting the Edit/Sampling Frequency menu in the SPTool window or by selecting the Analy- sis/Sampling Frequency menu in the FDATool window, which will be opened by clicking the Filters/Edit button.) If you start an FVTool by clicking the New button or by selecting File/New from within FVTool, that FVTool is a stand-alone version that is not linked to SPTool. You can also access a reduced version of FDATool (Fig. 7.24(a)) by clicking the New button to create a new filter or the Edit button to edit a selected filter. You can apply a selected filter to a selected signal by clicking the Apply button. Clicking the Create button opens the Spectrum Viewer and shows the PSD (power spectral density) of the selected signal. Clicking the Update button opens the Spectrum Viewer for the selected spectrum.

Let us try using the SPTool in the following steps:

1. Create a noisy two-tone signal in the MATLAB workspace and import it into SPTool.

2. Design a BPF using FDATool.

3. Apply the designed filter to the signal to create a bandlimited noisy signal.

4. Analyze the input and output signals. For example, you can compare the original and filtered signals in the time domain using the Signal Browser.

5. Compare the original and filtered signals in the frequency domain using the Spectrum Viewer.

6. Save the filter design and signal analysis results by exporting to disk (MAT-file) or workspace.

< Step 1: Creating a noisy signal in the MATLAB workspace and importing it into SPTool> You can type the following statements into the MATLAB command window:

>>Fs=1e4; Ts=1/Fs; tt=[0:Ts:0.5]; randn(’state’,0); >>x=sin(2*pi*941*tt)+cos(2*pi*1209*tt)+0.1*randn(size(tt)); >>sptool % Open a SPTool window to start a new SPTool session

Then in the SPTool window, select the File/Import menu to open the Import- to-SPTool dialog box in which you can import x and Fs into the SPTool in the following way (see Fig. 7.28(a)):

- Select x from the Workspace Contents list, click the upper Right-Arrow button, and name the signal by typing, say, ‘sig1’ (default name) in the Name field to import x as a signal data.

7.3 How to Use SPTool 353

Fig. 7.28 Importing signal, designing filter, applying designed filter, and editing sampling frequency

354 7 Analog and Digital Filters - Select Fs from the workspace contents list and click the lower Right-

Arrow button to import Fs as the sampling frequency. - Click the OK button to close the Import-to-SPTool dialog box. Then you

will see sig1[vector] in the SPTool’s Signals list (see Fig. 7.29(a)). (cf) You can also import signals from MAT-files on your disk.

(cf) You can import filters and spectra into SPTool in the same way as you import signals.

< Step 2: Using FDATool to design a BPF> You might import an existing filter or design/edit a new filter using FDATool. Here, click the New button to open FDATool, type the following specification into the appropriate fields, and then click the Design Filter button to design a filter named ‘filt1’ by default (Fig. 7.28(b)): Sampling frequency: Fs = 10kHz, Stopband attenuation: A s = 50dB, and Passband ripple: R p = 3dB

Passband edge frequencies: f p 1 = 1.1 kHz, f p 2 = 1.8 kHz Stopband edge frequencies: f s 1 = 1 kHz, f s 2 = 2kHz

< Step 3: Applying the designed filter to the signal to create a bandlimited signal>

- Select the signal ‘sig1[vector]’ from the Signals list by clicking on it (Fig. 7.27(a)). - Select the filter ‘filt1[design]’ from the Filters list by clicking on it. - Click the Apply button under the Filters list to open the Apply Filter dialog

box (Fig. 7.28(c)), in which you can select the filtering algorithm, name the output signal, say, ‘sig2’, and click OK to close the dialog box. Then you will see sig2[vector] in the SPTool’s Signals list.

(cf) If you see a warning message that the sampling frequencies for the sig- nal and filter are different, select the Edit/Sampling Frequency menu to make them equal and then apply the filter to the signal (Fig. 7.28(d)).

< Step 4: Comparing the original and filtered signals in the time domain in Signal Browser>

- Select the signals ‘sig1’ and ‘sig2’ from the Signals list by (Shift/Ctrl+) clicking on them.

- Click the View button under the Signals list to open the Signal Browser window and see the two signals in the time domain. - If needed or desired, you can click the Select Trace button to select one of the displayed signals and then click the Line Properties button to change the color and/or line style to display the selected signal.

7.3 How to Use SPTool 355

- You can also click the Play Selected Signal button to play the selected sig-

nal. You can click the Vertical Markers button to use the vertical Markers to select a portion of the signal you want to play. If you want to print the signals (or their spectra), click the Print button.

< Step 5: Compare the original and filtered signals in the frequency domain in Spectrum Viewer>

- In the SPTool window (Fig. 7.29(a)), select the signal ‘sig1[vector]’ from

the Signals list by clicking on it. - Click the Create button under the Spectra list to create a PSD ‘spect1’

corresponding to the selected signal ‘sig1’ and open the Spectrum Viewer window (Fig. 7.29(b1)). Note that the PSD is not yet computed and displayed.

- Through the Parameters region, set the parameters such as the spectral

analysis method (Welch), FFT size (Nfft = 1024), window size (Nwind = 1024), Window type (hanning), and Overlap length (512).

- Click the Apply button in the Spectrum Viewer window to compute and

display the PSD spect1. - Follow the above three steps for the filter output signal ‘sig2’ to create

another PSD, say, spect2. In the Parameters region, you had better select ‘spect1’ from the PSD list in the Inherit from field (Fig. 7.29(b2)) so that every spectral analysis parameter can be inherited from the existent PSD ‘spect1’.

- You can Shift + click on ‘spect1’ and ‘spect2’ in the Spectra list to select

them and click the View button under the Spectra list to reactivate the Spectrum Viewer and display the two spectra together.

- You can export the signals, filters, and spectra through the Export from

SPTool dialog box opened by selecting the File/Export menu.

< Step 6: Save the filter design and signal analysis results>

You can save the designed filter(s) and signal analysis result(s) by exporting on your disk (as a MAT-file) or the MATLAB workspace in the follow- ing way:

- In the SPTool window, select the File/Export menu to open the Export

from SPTool dialog box in which you can select the signals, filters, and spectra you want to save from the Export list and save them on your disk (as a MAT-file) or the MATLAB workspace by clicking the Export to Disk or Export to workspace button, respectively (see Fig. 7.30).

- If you have exported the filter structure ‘filt1’ into the workspace, you can

refer to its numerator and denominator as ‘filt1.tf.num’ and ‘filt1.tf.den’.

356 7 Analog and Digital Filters

Fig. 7.29 SPTool window and Spectrum Viewer window

Problems 357

Fig. 7.30 Export from SPTool dialog box

Problems

7.1 Design and Use of a Filter to Remove/Reduce a Noise from a Music Signal (a) Using any recording software, create a wave file of sampling frequency

8kHz, duration 16 s, and any melody that you like in the name of, say, “melody.wav”. Then take the following steps:

(1) Use the MATLAB command ‘wavread()’ to extract the signal vector x and sampling frequency Fs from the wave file. Use ‘soundsc()’ to listen to the melody signal. Use ‘fft()’ & ‘plot()’ to plot x together with the magnitude of its DFT spectrum in dB.

(2) Add a noise of amplitude 0.5 and frequency 3.9kHz to the signal, listen to the noise-contaminated signal xn, and plot xn together with its DFT spectrum magnitude in dB.

(3) Design a Butterworth LPF with passband/stopband edge frequencies of 3.6kHZ/3.9kHz and use the LPF to filter the noise-contaminated signal to obtain a filtered signal xf. Then listen to the filtered signal xf and plot xf together with the magnitude of its DFT spectrum in dB.

358 7 Analog and Digital Filters

%test filtering.m clear, clf [x,Fs]=wavread(’melody.wav’); N=2ˆ17; x=x(end-N+1:end,1).’; % convert into row vector soundsc(x,Fs); Ts=1/Fs; t=(0:N-1)*Ts; nn=1:N/32; tt=t(nn); % time vector subplot(4,2,1), plot(tt,x(nn)), axis([tt([1 end]) -2 2]) xlabel(’time[s]’), ylabel(’signal x[n]’) X=fftshift(fft(x,N)); X mag=20*log10(abs([X X(1)])); f=(-N/2:N/2)*(Fs/N); fkHz=f/1000; % frequency vector subplot(4,2,2), plot(fkHz,X mag), axis([-5 5 -40 100]) xlabel(’f[kHz]’), ylabel(’20*log10|X(k)|[dB]’) % Add a High Frequency Noise omega=2*pi*3900*Ts; % convert 3.9 kHz into digital (DT) frequency %omega=2*pi*5000*Ts; % convert 5 kHz into digital (DT) frequency n=0:N-1; noise=0.5*cos(omega*n); xn = x + noise; soundsc(xn,Fs); subplot(4,2,3), plot(tt,xn(nn)), axis([tt([1 end]) -3 3]) xlabel(’time[s]’), ylabel(’noise-contaminated signal xn[n]’) Xn=fftshift(fft(xn,N)); Xn mag=20*log10(abs([Xn Xn(1)])); subplot(4,2,4), plot(fkHz,Xn mag), axis([-5 5 -40 100]) xlabel(’f[kHz]’), ylabel(’20*log10|Xn(k)|[dB]’), % Butterworth LPF Design Rp=3; As=40; % Passband Ripple and Stopband Attenuation in dB fp=3600*Ts*2; fs=3900*Ts*2; % passband/stopband edge frequency [Nb,fcb]=buttord(fp,fs,Rp,As); [Bb,Ab]=butter(Nb,fcb); H=fftshift(freqz(Bb,Ab,N,’whole’)); H mag=20*log10(abs([H; H(1)])); subplot(4,2,6), plot(fkHz,H mag), axis([-5 5 -100 5]) xlabel(’f[kHz]’), ylabel(’20*log10|H(k)|[dB]’), % Filtering to remove the 10kHz noise xf=filter(Bb,Ab,xn);

soundsc(xf,Fs);

subplot(4,2,7), plot(tt,xf(nn)), axis([tt([1 end]) -2 2]) xlabel(’time[s]’), ylabel(’filetred signal xf[n]’) Xf=fftshift(fft(xf,N)); Xf mag=20*log10(abs([Xf Xf(1)])); subplot(4,2,8), plot(fkHz,Xf mag); axis([-5 5 -40 100]) xlabel(’f[kHz]’), ylabel(’20*log10|Xf(k)|[dB]’),

(b) Referring to Fig. P7.1, make a Simulink model file to perform the filtering operation as done by the above MATLAB program “test filtering.m”. You can relocate the To Wave Device block to listen to the original signal, the noise-contaminated one, and the filtered one.

Problems 359

Fig. P7.1 Simulink block diagram for a signal filtering and the parameter setting dialog boxes for spectrum scope block

“ This page left intentionally blank.”