from fourier series to analysis of non-stationary signals...
TRANSCRIPT
Short Time Fourier TransformSpectrograms
From Fourier Series to Analysis ofNon-stationary Signals - VII
prof. Miroslav Vlcek
November 23, 2010
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Contents
1 Short Time Fourier Transform
2 Spectrograms
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Contents
1 Short Time Fourier Transform
2 Spectrograms
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
An overview of Fourier transforms
continuous in time discrete in timeperiodic in frequency
infr
eque
ncy
x(t) =1
2π
∞∫
−∞
X(jω)ejωt dω x(n) =T
2π
+π/T∫
−π/T
X(ejωT)ejkωT dω
cont
inuo
us
X(jω) =
∞∫
−∞
e−jωt x(t)dt X(ejωT ) =∞∑
n=−∞
x(n)e−jkωT
Fourier transform Fourier transform t = nT
infr
eque
ncy
intim
e
x(t) =∞∑
k=−∞
X(k)ejkω0t x(n) =1
N
N−1∑k=0
X(k)e(j2π/N)kn
disc
rete
peri
odic
X(k) =ω0
2π
π/ω0∫
−π/ω0
x(t)e−jnω0t dt X(k) =N−1∑n=0
x(n)e−(j2π/N)kn
Fourier series Discrete Fourier transform
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
What is wrong with Fourier Transform?
Lets consider basis functions: sin(ωt), cos(ωt) and δ(t)
support region in time in frequency
sin(ωt) (−∞,∞) 0cos(ωt) (−∞,∞) 0δ(t) 0 (−∞,∞)
• The basis functions sin(ωt) and cos(ωt) are not localizedin time!
• The δ(t) is not localized in frequency!
We have learnt to localize a signal in time domain by windowing⇒ short time Fourier transform
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Short Time Fourier Transform
• When sampling an audio signal at a sampling rate44.1 kHz
• ... 1 hour of stereophonic music would be44100 × 2 × 60 × 60 = 317520000 samples !
• If we want to compute DFT, the closest power-of-two FFTis 228 = 268435456 per channel.
• A better approach is to break the long signal into smallsegments and analyze each one with FFT ⇒ short timeFourier transform
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Short Time Fourier Transform
• Assume that x(m) is an infinitely long sequence
• in order to localize energy in time and frequency wesegment the signal into short-time pieces and calculateDFT of each one
• sampled STFT for a window defined in the region0 ≤ m ≤ M − 1 is given by
X (k , ℓL) =M−1∑
m=0
x(ℓL − m)w(m)exp(
−j2πkmN
)
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Short Time Fourier Transform
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Short Time Fourier Transform
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Spectrograms
In MATLAB the command
spectrogram(x,window,noverlap,nfft,fs,’yaxis’)
performs short-time Fourier transform and plots a 2Dfrequency-time diagram, where
• x is the signal specified by vector x• window is a Hamming window of length nfft
• noverlap is the number of overlapping segments thatproduces 50% overlap between segments
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Spectrograms
• nfft is the FFT length and is the maximum of 256 or thenext power of 2 greater than the length of each segment ofx
• fs is the sampling frequency, which defaults to normalizedfrequency
• using ’yaxis’ displays frequency on the y-axis and timeon the x-axis
We also use command colorbar which appends a color scaleto the current axes.
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
Chirp signal analysis sin(2π(f0 + αt))
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram I
1 Start MATLAB. Load in the audio signal with commandsfilename = ’flute-C4.wav’;[x1 sr1] = wavread(filename);
2 The sampling rate is 11025 Herz, and the signal contains36 250 samples. If we consider this signal as sampled onan interval (0,T ), what is the time duration of the flutesound ?
3 Use command soundsc(x1,sr1) to obtain flute soundclick to play
4 Make resampling of the audio signal by fr = 4000[Hz] andwrite the sound file to disk usingwavwrite(x2, sr2, ’flute-resampled.wav’);
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram I
0 0.5 1 1.5 2 2.5 3 3.5
−0.05
0
0.05
Flute
Time [s]
0 0.5 1 1.5 2 2.5 3 3.5
−0.05
0
0.05
Resampled flute
Time [s])
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram I
5 Compute the DFT of the signal withX1 = fft(x1(1:1024)); and X2 =fft(x2(1:1024));
6 DFT of real-valued signals is always symmetric aroundsr/2 so we only need to plot the first half. Display themagnitude of the Fourier transform usingplot(f1(1:end/2+1), abs(X1(1:end/2+1)))
7 Q: What is the approximate fundamental frequency of theflute note C4?
8 A: Find the bin corresponding to the first peak in themagnitude spectrum.
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram I
9 You can use a systematic way to find the frequency of thepeaks in spectrum abs(X2)using following commands:% find local maximamag = abs(X2);mag = mag(1:end/2+1);peaks = (mag(1:end-2) < mag(2:end-1)) &mag(2:end-1) > mag(3:end);
10 Then evaluate the peaks at corresponding frequenciesabove a threshold:peaks = peaks & mag(2:end-1) > 0.5;fmax=f2(peaks)
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram I
0 1000 2000 3000 4000 5000 60000
0.2
0.4
0.6
0.8
1
1.2
1.4
Mag
nitu
de
Frequency (Hz)
Flute
0 500 1000 1500 2000 25000
1
2
3
4
5
6
7
Mag
nitu
de
Frequency (Hz)
Resampled flute
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram I
11 Finally we will use Spectrogram with folowingspecifications:nwin = 512; % samples of a windownoverlap = 256; % samples of overlapsnfft = 512; % samples of fast Fouriertransformspectrogram(x1, nwin, noverlap, nfft, sr2,’yaxis’);colorbarprint -djpeg90 figure-spect
12 Carefully study the options for the spectrogram using help!
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram I
0.5 1 1.5 2 2.5 30
200
400
600
800
1000
1200
1400
1600
1800
2000
Time
Fre
quen
cy (
Hz)
−140
−130
−120
−110
−100
−90
−80
−70
−60
−50
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram II
1 Construct a signal f of two sinusoids on intervalt ∈< 0,2 > sampled at 1 kHzt=[0:1999]/1000;f=cos(2*pi*137*t)+cos(2*pi*147*t);
2 Compute the DFT of f and display its magnitude in timeand frequency domain.
3 Use spectrogram routinespectrogram(f,512,510,512,1000,’yaxis’);colorbar
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram II
prof. Miroslav Vlcek Lecture 7
Short Time Fourier TransformSpectrograms
MATLAB project Spectrogram II
1 Construct a signal f with varying frequency within intervalt ∈< 0,2 > sampled at 1 kHzt=[0:1999]/1000;arg=100+20*cos(2*pi*t);f=cos(2*pi*arg.*t);
2 Compute the DFT of f and display its magnitude in timeand frequency domain.
3 Use spectrogram routinespectrogram(f,128,120,512,1000,’yaxis’);colorbar
prof. Miroslav Vlcek Lecture 7