+ All Categories
Home > Documents > lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using...

lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using...

Date post: 13-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
21
2012
Transcript
Page 1: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

  

                                    

  

2012

Page 2: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Digital Signal Processing ETI265 2012

Introduction In the course we have 2 laboratory works for 2012. Each laboratory work is a 3 hours lesson. We will use MATLAB for illustrate some features in digital signal processing. Equipment: PC with Matlab and input/output of sound. Matlab: Matlab toolboxes: Signal Processing toolbox Data acquisition toolbox Lab 1: Real time spectral analysis using Fourier transform and estimation of impulse responses using correlation function Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses using correlation Task 4. Modulation Task 5: SSB-modulator Lab 2: Design of IIR-filters Task 1: Relation between poles and filter spectrum Task 2: Design of IIR-filter from filter specification Task 3 Notch filters Task 4: Filter music signals

Page 3: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Laboratory work 1: Real time spectral analysis using the Fourier transform In this laboratory work we will use MATLAB for illustrate some features in digital signal processing. Start Matlab and update the Matlab path. Connect the microphone to PC ’ mic in’ connector. You can use the speakers in the PC or connect external headphones. Task 1. Real time spectral analysis using Fourier transform Start Matlab. At the Matlab prompt, type sigfftio_lin (or sigfftio_log or demoai_fft’) Now a spectrum analyzing window will be opened, see below

Item 1: Say the word ‘smile’ slowly and look at the spectra of the vowels ‘i’ and ‘e’. Fill in the diagram below and estimate the pitch. My pitch is…………...(some mean value). Item 2: Try to generate a sound with as flat spectrum as possible. (‘oral white noise’)

Page 4: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Task 2. Real time spectrogram A spectrogram is a time-frequency plot of the signal. A sliding window is applied to the signal and the short time Fourier transform are determined for each time-window. The plot has time on the x-axis and frequency on the y-axis. See help spectrogram for more information Type N=20000; Fmax=4000; record_spectra_china % N=no of samples (Fs=10 kHz),

%Fmax = max freq in the plot (<5000 Hz) Now 2 seconds of ‘mic in’ will be recorded and then the spectrogram will be shown. (Increase N if you want to have longer sequences)

Item 1: Pronounce the Chinese words below and see if you have the correct

pitch (tone). Let the Chinese students show the correct pitch.

Use also some Swedish words with Chinese tone.

Page 5: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Task 3. Estimation of impulses responses using correlation Correlation functions are often used in estimation of unknown systems. This is described in the textbook pages 99-101 and in the slides from the second lecture.. Input – Output Relations using correlation functions (from the slides)

   Autocorrelation function for the output 

    )()()()()()( lhlhlriflrlrlr hhxxhhyy −∗=∗=  Cross correlation function for input‐output signal 

    )()()( lrlhlr xxyx ∗=  

If the autocorrelation for the input is a delta function, )()( llrxx δ= , we

direct have the impulse response )()( lrlh yx= .

If the autocorrelation for the input is not a delta function, we have to estimate the impulse response from the expression for the cross correlation. This is not included in this course. But still, we can find a lot of information even in this case. We will here use pre-generated signals. In files on the computer there are pairs of input and output signals from various unknown filters. Try to estimate these impulse responses. Matlab script for computing the correlation function used below. This script exist in the Matlab path. % lab_sigcorr.m Compute and plot correlation -N0<0<N0 % [rxy,n]=lab_sigcorr(x,y,N0); function [rxy,n]=lab_sigcorr(x,y,N0) Nx=length(x); Ny=length(y); if Nx==Ny N=Nx; else N=min(Nx,Ny); end rxy=xcorr(x(1:N),y(1:N)); rxy=rxy(N-N0:N+N0); n=-N0:N0;

h(n) y(n) = h(n) * x(n) x(n)  

Page 6: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Item 1: Music through an echo filter. First we listen to music through an echo filter (reverberation). We can hear the effect of the echoes but it will be difficult to estimate the time delays. The input and output signals are stored in files with input in the left channel and output in the right channel. Listen to the signals and then try to estimate the delay using correlation functions. Type the command below Load the signals and plot and listen to them. load sig_music; x=sig_music(:,1); y=sig_music(:,2); % load input and output signals soundsc(x,10000); pause(10), soundsc(y,10000); Use also the plot command to plot parts of the signals. subplot(211), plot(x), subplot(212), plot(y) % Plot input and output Now, use correlation functions to estimate the delays (the impulse response). Write type sigcorr to show the Matlab code. subplot(211), N0=2000;[rxx,n]=sigcorr(x,x,N0);plot(n,rxx);grid on % input auto correlation subplot(212), N0=2000;[ryx,n]=sigcorr(y,x,N0);plot(n,ryx);grid on % cross correlation Estimate the delay in the impulse response. The delays are ……...ms (my guess). Item 2: Use white noise as the input to the echo filter. We use white noise as the input and repeat the instructions from item 1. White noise has the correlation equal to a delta spike, i.e. the autocorrelation function for white noise is rxx(l)=δ(l). Type the command below subplot(211), N0=2000;[rxx,n]=sigcorr(x,x,N0);plot(n,rxx);grid on % input auto correlation subplot(212), N0=2000;[ryx,n]=sigcorr(y,x,N0);plot(n,ryx);grid on % cross correlation % Load input and output data load sig_noise; x=sig_noise(:,1);y=sig_noise(:,2); Note: Decrease the pause to 4 s. soundsc(x,10000);pause(4),soundsc(y,10000); % Listen to input and output Use also the plot command to plot parts of the signals. subplot(211), plot(x), subplot(212), plot(y) % Plot input and output Now, use correlation functions to estimate the delas (impulse response). subplot(211), N0=2000;[rxx,n]=sigcorr(x,x,N0);plot(n,rxx);grid on % input auto correlation subplot(212), N0=2000;[ryx,n]=sigcorr(y,x,N0);plot(n,ryx);grid on % in-out cross correlation

Page 7: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

With white noise as input it will be easier to estimate the impulse response. The estimate the delays in the impulse response are ….............................…...ms. Item 3: White noise through a band pass filter. We have now estimated the impulse response of an echo filter. Next step is to estimate a band pass filter impulse response. Load the input-output signals and listen to them. Then estimate the impulse response using correlation functions. Type the command below load sig_bandpass_noise; x=sig_bandpass_noise(:,1);y=sig_bandpass_noise(:,2); soundsc(x,10000);pause(4),soundsc(y,10000); subplot(211), N0=2000;[rxx,n]=sigcorr(x,x,N0);plot(n,rxx);grid on % input auto correlation subplot(212), N0=2000;[ryx,n]=sigcorr(y,x,N0);plot(n,ryx);grid on % cross correlation You can hear that the output signal is a narrow band signal. Check the spectrum by taking the Fourier transform of ryx(n) by typing sigfftp(ryx,10000,10000,1000); % plot spectra up to 1 kHz, Fs=10 kHz Task 4: Modulation In most communication systems the signals are transladed from a lower frequency band up to a higher frequencies. This is called modulation. We know the formula

{ { )cos()cos()cos()cos(2difference

bababasum

−++=

This means that when we multiply two signals we will have the sum and difference of the angles. When we have signals, this formula is )))(2cos())(2cos((5.0)2cos()2cos( cmcmcm ffffff −++= ππππ The multiplication gives then one signal at the frequency cmlower fff += and one at the frequency cmupper fff += and this frequencies is called the lower and the upper side band.

cos(2π 0.25 n) 

x(n)  y(n)

Page 8: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

An example of the frequency contents is shown below with )05.02sin()( nnx ⋅= π . Then, the input spectrum and the output spectrum are shown below.

Item 1: Check this in Matlab by typing: N=20000;n=1:N;x=sin(2*pi*0.05*n); speech_modulation_lab_a Item 2: Now, change the input frequency to f=0.1, i.e. )1.02sin()( nnx ⋅= π and fill in the diagram below.

Page 9: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Item 3: Speech scrambler A special case of the above example occurs then we have the figure below. This is often called speech scrambling in the literature. You shall test this system in Matlab . Item 1: Test the system with sound as input. Follow the instructions below. load sig_music; x=sig_music(:,1); %load sound signal x=sig_music(:,1); N=length(x); n=[1:N]'; speech_scrambler_lab_a % speech scrambler Try to explain the results from Matlab. Matlab scrip for speech scrambler %speech_scrambler_lab_a.m demo of speech scrambler bm 2011 % Use: N=20000;n=1:N;x=sin(2*pi*0.1*n); speech_scrambler_lab_a y=(-1).^n.*x; sound(0.5*x,10000);pause(10);sound(0.5*y,10000) subplot(211),sigfftp(x,1,N,1); subplot(212),sigfftp(y,1,N,1); Item 2: To further analyze the system we use sinusoids as the input, )1.02cos()( nnx π= . Test the speech scrambler with this signal. First, fill in the spectra in the figure below. Spectrum |X(f)| and |Y(f)|.

Check your solution by typing N=4000; n=[1:N]'; x=sin(2*pi*0.1*n); speech_scrambler_lab_a

cos(2π 0.5 n)=(‐1)n

x(n)  y(n)

Page 10: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Task 5: SSB-modulator I all communication systems, frequency translation are used. We move information signals from low frequencies up to higher frequencies before transmission and at the receiver side we received the signal and then translate the signals back to low frequencies. This procedure is called modulation-demodulation and the equipment is called modem. We illustrate this with an SSB-modulation-demodulation system often used in communication systems. A time-discrete system is given below (SSB modulator). Test this using the Matlab script below. %ssb_lab_2011.m demo of SSB-mod/demoulation bm 2011 % Use: N=20000;x=sin(2*pi*0.1*n);deltaf=0.025;,ssb % Use: N=20000;n=[1:N]'; x=sin(2*pi*0.1*n); deltaf=0;, ssb_lab_2011 % x=sig_music(:,1);N=length(x);deltaf=0.025;,ssb_lab_2011, soundsc(xe,10000); fc=.25; n=[1:N]'; hlp=fir1(500,2*.25); xa=x; xb=xa.*sin(2*pi*fc*n); xc=filter(hlp,1,xb); xd=xc.*cos(2*pi*(fc+deltaf)*n); xe=filter(hlp,1,xd); subplot(511),sigfftp(xa,1,N); subplot(512),sigfftp(xb,1,N); subplot(513),sigfftp(xc,1,N); subplot(514),sigfftp(xd,1,N); subplot(515),sigfftp(xe,1,N);

1 0.75 f0.25  0.5

| Hlow pass (f)| 

Hlowpass(f)Hlowpass(f)C 

y(n)x(n) 

B  D  E A 

cos(2π 0.25 n)  cos(2π 0.(25 +deltaf) n) 

Page 11: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Item 5.1: Music through the SSB system First, test the SSB modulator/demodulator with music (deltaf=0). Type load sig_music; x=sig_music(:,1); soundsc(x,10000); x=sig_music(:,1);N=length(x);deltaf=0.0; ssb_lab_2012, soundsc(xe,10000); The figure below shows the spectra in the points A, B, C, D and E. Note that the x-axis is 0<f<0.5.

Item 5.2. A sinousoids through the the SSB modulator/demodulator. To analyze the SSB system, we now use a sinusoids as input signal (deltaf=0),

)1.02sin()( nnx ⋅= π Fill in the spectra in the points A, B, C, D and E in the figure below.

Page 12: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Then check the result in Matlab using N=20000;n=[1:N]'; x=sin(2*pi*0.1*n); deltaf=0; ssb_lab_2012 Item 5.3: Demodulation with wrong frequency Finally, we check the case when deltaf=0.02, i.e. we use the wrong frequency in the last step in the demodulator. Type x=sig_music(:,1);N=length(x);deltaf=0.02; ssb_lab_2012, soundsc(xe,10000); Explain what's happen in the SSB system for various values of deltaf. The spectra is shown below for deltaf=0.02.

Page 13: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Laboratory work 2: Design of IIR filters Introduction In this laboratory work we will design FIR and IIR filters. First, we will show the relation between pole-zero plots and the magnitude spectra and impulse responses for digital filters. The we will design notch filters which will be tested in both Matlab Preparation exercise 1 Combine the pole-zero plot and the magnitude spectra |H(f)| below. Show the pair of pole-zero plot and magnitude spectra corresponding to the same system. Pole-zero plot Magnitude spectra |H(f)|

Page 14: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Preparation exercise 2 Sketch the magnitude spectra |H(f)| for the given the pole-zero plots. Pole-zero plot Magnitude spectra |H(f)|

Page 15: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Laboratory tasks IIR filter The program MKIIR is used for interactive IIR filter design. You can place poles and zeros in the pole-zero plane and then show the impulse response, magnitude spectra and phase function for the digital system. The program can also design filters from specification of requirements of magnitude spectra. A set of standard filters are also included in the program.

Task 1: Relation between poles and filter spectrum Type MKIIR to start the program at the Matlab prompt. Place a pole or a pair of complex poles in the z plane and check the result. Press first Replace last. Move the pole inside the unit circle and check the result. Specially, look at the relation of the magnitude and angle of the poles and the magnitude spectra and the impulse response. Release Replace last.

Page 16: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Task 2: Design of IIR-filter from filter specification Choose poles and zeros for fulfilling the demands below based on your experience from task 1. Press Import Workspace and open the file irspec1.mat to load the specifications into MKIIR.

Now, choose poles and zeros to fulfill the requirements. You can ave your filter with Export Workspace and choose a file name (i.e.filter1.mat. Then press Import filter to reload the filter. Optional task Compare with standard filter solutions. Stop the program MKIIR, press Exit. Task 3 Notch filters Now, you will examine notch filters. The system function for both the FIR filter and the IIR filter are given below

with 0ω the frequency which shall be cancelled. Plot the magnitude spectra for the filters with varying position of the poles and zeros using the program MKIIR . Alternatively, you can use Matlab direct and typing your system function above (FIR). f0=0.2; r=0.95; w0=2*pi*0.2; f=0:.01:1; z=exp(j*2*pi*f); Hfir=1-2*cos(w0)*z.^(-1)+z.^(-2);subplot(211), plot(f,abs(Hfir)); Hiir=Hfir./(1-2*r*cos(w0)*z.^(-1)+r^2*z.^(-2)); subplot(212),plot(f,abs(Hiir));

Page 17: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Task 4: Filter music signals In this task, you shall cancel sinusoids added to music files. Sampling rate Fs=8000 Hz, stereo. Call global S, jukebox, song=S; and choose a song from the list. You must choose one of the first 5 for testing in the DSP. The variable S will contents 100000 samples from the song and it is stored in the variable 'song' for future use. Listen to the song (small part) soundsc(song,8000); Use Windows "Sound recorder" to listen to the whole song. Avoid long files in Matlab due to difficulties to stop the sound outputs in Matlab. Now, use mkspa to determine the frequencies of the sinusoids. Press PEAK and use the mouse to point at peaks in the spectrum. The frequencies are multiples of 10 Hz. Write down the frequencies for the peaks of your song. Press Exit to stop mkspa. Now, you shall determine a notch filter which cancel the sinusoids. Try both FIR and IIR filters. Type your choice of poles and zeros in Matlab and put them into vectors. Then, determine the A and B polynomials. Example: Fs=8000; F1= ; F2= ; F3= ; z1=exp(-j*2*pi*F1/Fs); z2=conj(z1); z3=exp(-j*2*pi*F2/Fs); z4=conj(z3); z5=exp(-j*2*pi*F3/Fs); z6=conj(z5); Z=[z1,z2,z3,z4,z5,z6]; P=0.9*Z; B=poly(Z) A=poly(P) zplane(B,A) % check pole-zero plot plot also the magnitude spectrum for your filter (see help freqz)

f=0:.01:1; w=2*pi*f; H=freqz(B,1,w); plot(f,abs(H)); %FIR f=0:.01:1; w=2*pi*f; H=freqz(B,A,w); plot(f,abs(H)); %IIR

Notice: You must use j for the imagine part. Test the filter using MKIIR.

Page 18: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Test the filter in Matlab You have already a small part of the song in the global variable 'song'. If not, reloaded using: song=wavread('ssilence',[1 100000]); % load 100 000 value Listen to the disturbed song (no filtering) soundsc(song, 8000); % listen Fs=8 kHz Filter the song with your filters, both the FIR and the IIR filter and listen. y0=filter(B,1,song); soundsc(y0, 8000); y1=filter(B,A,song); soundsc(y1, 8000); Did your filters fulfill the requirements?

Page 19: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Appendix: Program descriptions IIR filter design, MKIIR MKIIR can be used for interactive filter design from poles and zeros.

Function Import Workspace: Read filter specifications from file. Export Workspace: Save filter specifications on file. Set filter specs: Input filter specifications. Filter coeffs: Input filter coefficients. Help: Not implemented yet. About: Program version. Reset: Resets poles and zeros but not filter specifications. Exit: Exits and save workspace to the file current.mat . Add pole: Place pole, press left mouse button, ends with

right mouse button. Delete pole: Place cursor over pole, press left mouse

button to delete, ends with right mouse button. Add zero: Set Add pole Delete zero: See delete pole

Page 20: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Move: See add pole Import filter: Import of standard filters from Matlab

Signal Processing Toolbox. Magnitude: Magnitude function. Phase: Phase function. Impulse: Impulse response. Replace last: Replace last Reciproc Zeros: HighPass: High pass filter |H(f)|_{| f=0.5}=1,

Low pass filter |H(f)|_{| f=0}=1. Zoom: Zoom. Unwrap; Phase Unwrap Phase. Focus: Radius Adjust Gain: Adjust Gain in dB.

Page 21: lab china 2012 lab matlab - Lunds tekniska högskola · Task 1. Real time spectral analysis using Fourier transform Task 2. Real time spectrogram Task 3. Estimation of impulses responses

Spectral analysis, MKSPA MKSPA is used to analyze the spectra in the sound files.

Functions Window: Window functions. FFT length: FFT length. Overlap: Number of overlapping segments Fs(Hz): Sampling frequency. Zoom: Activate/deactivate Matlab zoom Analyze: Analyze Exit End the program.     


Recommended