Useful Matlab

Embed Size (px)

Citation preview

  • 7/31/2019 Useful Matlab

    1/17

    Lecture 1: Matlab DSP Review

    Contents

    Simple signalsAudio I/O

    ResamplingFourier Transform

    FiltersSpectrograms

    Getting help

    Simple signals

    Generate a pure tone.

    % Pick a sampling rate.sr = 8000;% Define the time axis.dur = 1; % sect = linspace(0, dur, dur * sr);freq = 440; % Hzx = sin(2*pi*freq*t);

    Playing sounds.

    soundsc(x, sr)

    Warning: The playback thread did not start within one second.(Type "warning off MATLAB:audioplayer:playthread" to suppress this warning.)

    Making plots.

    plot(t, x)xlabel('Time (sec)')

    http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#1http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#1http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#7http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#7http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#8http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#8http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#10http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#10http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#15http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#15http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#23http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#23http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#24http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#24http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#24http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#23http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#15http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#10http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#8http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#7http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html#1
  • 7/31/2019 Useful Matlab

    2/17

    That looks pretty terrible. Lets only plot the first few cycles.

    period = 1.0 / freq; % secsamples_per_period = period * sr;

    idx = 1:3*samples_per_period;plot(t(idx) * 1e3, x(idx));xlabel('Time (msec)')

  • 7/31/2019 Useful Matlab

    3/17

  • 7/31/2019 Useful Matlab

    4/17

    Audio I/O

    filename = 'flute-C4.wav';[x2 sr2] = wavread(filename);

    % Get the time axis right.t2 = linspace(0, length(x2) / sr2, length(x2));plot(t2, x2)xlabel('Time (sec)')

  • 7/31/2019 Useful Matlab

    5/17

    Resampling

    sr3 = 4000; % Hzx3 = resample(x2, 8000, sr3);subplot(211)title('Flute')plot(x2)subplot(212)plot(x3)title('Resampled flute')xlabel('Time (samples)')

  • 7/31/2019 Useful Matlab

    6/17

    Write the sound file to disk.

    wavwrite(x3, sr3, 'flute-resampled.wav')

    Fourier Transform

    X = fft(x);

    Whats the frequency axis?

    f = linspace(0, sr, length(X));subplot(211)plot(f, abs(X))title('Magnitude')subplot(212)plot(f, angle(X))title('Phase')xlabel('Frequency (Hz)')

  • 7/31/2019 Useful Matlab

    7/17

    Recall that for real-valued signals the DFT is always symmetric around sr/2 (or 0), so we onlyneed to plot the first half. This time for x2.

    X2 = fft(x2(1:1024));

    f2 = linspace(0, sr2, length(X2));subplot(211)plot(f2(1:end/2+1), abs(X2(1:end/2+1)))title('Magnitude')subplot(212)plot(f2(1:end/2+1), angle(X2(1:end/2+1)))title('Phase')xlabel('Frequency (Hz)')

  • 7/31/2019 Useful Matlab

    8/17

    Whats the (approximate) fundamental frequency of the flute note (C4)? Just find the bincorresponding to the first peak in the magnitude spectrum.

    mag = abs(X2);

    % Ignore redundant second half.mag = mag(1:end/2+1);% Find all local maxima.peaks = (mag(1:end-2) < mag(2:end-1)) & mag(2:end-1) > mag(3:end);length(peaks)ans =

    511

    But we only want the peaks above a threshold to ensure that we ignore the noise.

    peaks = peaks & mag(2:end-1) > 0.5;f2(peaks)ans =

    247.87

    Filters

    Simple FIR filter: y[n] = 0.5*x[n] + 0.5*x[n-1]

    filt = [0.5, 0.5];

  • 7/31/2019 Useful Matlab

    9/17

    Lets filter some noise buy convolving it with the impulse response.

    % Recall that the convolution of two signals with length N1 and N2 is% N1 + N2 - 1. The 'same' argument just tells conv to truncate the% convolved signal at N1 samples.y = conv(n, filt, 'same');

    N = fft(n);Y = fft(y);idx = 1:length(n) / 2 + 1;subplot(211); plot(f(idx), abs(N(idx)))subplot(212); plot(f(idx), abs(Y(idx)))xlabel('Frequency (Hz)')

    What about IIR filters?

    [b a] = butter(2, [400 1500]/sr)y = filter(b, a, n);Y = fft(y);

    subplot(212); plot(f(idx), abs(Y(idx)))xlabel('Frequency (Hz)')b =

    0.035438 0 -0.070875 0 0.035438a =

    1 -3.2428 4.0778 -2.3717 0.54317

  • 7/31/2019 Useful Matlab

    10/17

    (You can use filter for FIR filters too, just be sure that the second argument is a scalar).

    Matlab has a special function to plot a filter's frequency response.

    clffreqz(b, a)

  • 7/31/2019 Useful Matlab

    11/17

    Pole-zero plots too:

    zplane(b, a)

  • 7/31/2019 Useful Matlab

    12/17

  • 7/31/2019 Useful Matlab

    13/17

    That pole is definitely outside of the unit circle. What is its impulse response?

    [h, t] = impz(b, a, 100); % Get first 100 samples of impulse responseplot(t, h)

    title('h[n]')xlabel('Time (samples)')

  • 7/31/2019 Useful Matlab

    14/17

    Spectrograms

    nwin = 512; % samplesnoverlap = 256; %samplesnfft = 512; %samplesspectrogram(x2, nwin, noverlap, nfft, sr2, 'yaxis');colorbar

  • 7/31/2019 Useful Matlab

    15/17

    Getting help

    lookfor spectrogramspecgram - Spectrogram using a Short-Time FourierTransform (STFT).spectrogram - Spectrogram using a Short-Time FourierTransform (STFT).specgramdemo - Spectrogram Display.help spectrogramSPECTROGRAM Spectrogram using a Short-Time Fourier Transform (STFT).

    S = SPECTROGRAM(X) returns the spectrogram of the signal specified byvector X in the matrix S. By default, X is divided into eight segmentswith 50% overlap, each segment is windowed with a Hamming window. Thenumber of frequency points used to calculate the discrete Fouriertransforms is equal to the maximum of 256 or the next power of twogreater than the length of each segment of X.

    If X cannot be divided exactly into eight segments, X will be truncatedaccordingly.

    S = SPECTROGRAM(X,WINDOW) when WINDOW is a vector, divides X intosegments of length equal to the length of WINDOW, and then windows eachsegment with the vector specified in WINDOW. If WINDOW is an integer,X is divided into segments of length equal to that integer value, and aHamming window of equal length is used. If WINDOW is not specified, thedefault is used.

  • 7/31/2019 Useful Matlab

    16/17

    S = SPECTROGRAM(X,WINDOW,NOVERLAP) NOVERLAP is the number of sampleseach segment of X overlaps. NOVERLAP must be an integer smaller thanWINDOW if WINDOW is an integer. NOVERLAP must be an integer smallerthan the length of WINDOW if WINDOW is a vector. If NOVERLAP is notspecified, the default value is used to obtain a 50% overlap.

    S = SPECTROGRAM(X,WINDOW,NOVERLAP,NFFT) specifies the number offrequency points used to calculate the discrete Fourier transforms.If NFFT is not specified, the default NFFT is used.

    S = SPECTROGRAM(X,WINDOW,NOVERLAP,NFFT,Fs) Fs is the sampling frequencyspecified in Hz. If Fs is specified as empty, it defaults to 1 Hz. Ifit is not specified, normalized frequency is used.

    Each column of S contains an estimate of the short-term, time-localizedfrequency content of the signal X. Time increases across the columnsof S, from left to right. Frequency increases down the rows, startingat 0. If X is a length NX complex signal, S is a complex matrix withNFFT rows and k = fix((NX-NOVERLAP)/(length(WINDOW)-NOVERLAP)) columns.For real X, S has (NFFT/2+1) rows if NFFT is even, and (NFFT+1)/2 rows

    if NFFT is odd.

    [S,F,T] = SPECTROGRAM(...) returns a vector of frequencies F and avector of times T at which the spectrogram is computed. F has lengthequal to the number of rows of S. T has length k (defined above) andits value corresponds to the center of each segment.

    [S,F,T] = SPECTROGRAM(X,WINDOW,NOVERLAP,F,Fs) where F is a vector offrequencies in Hz (with 2 or more elements) computes the spectrogram atthose frequencies using the Goertzel algorithm. The specifiedfrequencies in F are rounded to the nearest DFT bin commensurate withthe signal's resolution.

    [S,F,T,P] = SPECTROGRAM(...) P is a matrix representing the PowerSpectral Density (PSD) of each segment. For real signals, SPECTROGRAMreturns the one-sided modified periodogram estimate of the PSD of eachsegment; for complex signals and in the case when a vector offrequencies is specified, it returns the two-sided PSD.

    SPECTROGRAM(...) with no output arguments plots the PSD estimate foreach segment on a surface in the current figure. It usesSURF(f,t,10*log10(abs(P)) where P is the fourth output argument. Atrailing input string, FREQLOCATION, controls where MATLAB displays thefrequency axis. This string can be either 'xaxis' or 'yaxis'. Settingthis FREQLOCATION to 'yaxis' displays frequency on the y-axis and timeon the x-axis. The default is 'xaxis' which displays the frequency onthe x-axis. If FREQLOCATION is specified when output arguments are

    requested, it is ignored.

    EXAMPLE 1: Display the PSD of each segment of a quadratic chirp.t=0:0.001:2; % 2 secs @ 1kHz sample ratey=chirp(t,100,1,200,'q'); % Start @ 100Hz, cross 200Hz at t=1secspectrogram(y,128,120,128,1E3); % Display the spectrogramtitle('Quadratic Chirp: start at 100Hz and cross 200Hz at t=1sec');

    EXAMPLE 2: Display the PSD of each segment of a linear chirp.t=0:0.001:2; % 2 secs @ 1kHz sample rate

  • 7/31/2019 Useful Matlab

    17/17

    x=chirp(t,0,1,150); % Start @ DC, cross 150Hz at t=1secF = 0:.1:100;[y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis');% NOTE: This is the same as calling SPECTROGRAM with no outputs.surf(t,f,10*log10(abs(p)),'EdgeColor','none');axis xy; axis tight; colormap(jet); view(0,90);xlabel('Time');ylabel('Frequency (Hz)');

    See also PERIODOGRAM, PWELCH, SPECTRUM, GOERTZEL.

    Reference page in Help browserdoc spectrogram