Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the...

34
6.003: Signal Processing Using the Discrete Fourier Transform March 14, 2019

Transcript of Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the...

Page 1: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

6.003: Signal Processing

Using the Discrete Fourier Transform

March 14, 2019

Page 2: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Frequency Resolution

How many Fourier components are in a signal?

What are their amplitudes and frequencies?

Example: cello

Listen to the wav file DEb3.wav (fs = 44,100 Hz).

How can we characterize the frequency components?

Page 3: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Frequency Resolution

Extract 1024 samples and calculate DFT (using FFT algorithm).

x[n]∣∣X1[k]

∣∣

Does it matter which part of the wav file we take the samples from?

Why use 1024 samples?

Would more be better? Would fewer be better?

Page 4: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Frequency Resolution

Most of structure is in low frequencies. Zoom in on that part.

x[n]∣∣X1[k]

∣∣

What value of k corresponds to the big peak?

What is corresponding value of frequency (in Hz)?

Page 5: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Frequency Resolution

Most of structure is in low frequencies. Zoom in on that part.

x[n]∣∣X1[k]

∣∣

What value of k corresponds to the big peak? 7What is corresponding value of frequency (in Hz)?

koN/2 = fo

fs/2

fo = koN fs = 7

1024 × 44100 ≈ 301.46 Hz:

between D (293.66 Hz) and E flat (311.13 Hz)

Page 6: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Improving Frequency Resolution

The DFT provides integer resolution in k. Therefore, the peak at

k = 7 could be off by as much as ±12 .

To improve frequency resolution, increase length of analysis window.

Two methods

• Zero padding

− better assessment of peak frequency.

− does not improve resolution of multiple components

• Longer sequence

− improves resolution of multiple components

− averages frequency content over longer period (bad if signal

changes with time)

Page 7: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Original (N=1024).

x1[n]∣∣X1[k]

∣∣

Page 8: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Lengthen by a factor of 2 (N=2048).

x2[n]∣∣X2[k]

∣∣

What (if any) relations exist between |X2[k]| (this slide) and |X1[k]|(previous slide)?

Page 9: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Lengthen by a factor of 2 (N=2048).

x2[n]∣∣X2[k]

∣∣

What (if any) relations exist between |X2[k]| (this slide) and |X1[k]|(previous slide)?

X2[2k] = 12X1[k]

Page 10: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Lengthen by a factor of 4 (N=4096).

x3[n]∣∣X3[k]

∣∣

Page 11: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Lengthen by a factor of 8 (N=8192).

x4[n]∣∣X4[k]

∣∣

The stem plots can be distracting when they are close together.

(They also take a long time to compute!)

Replot using lines (but remember that the signals are DT).

Page 12: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Original (N=1024).∣∣X1[k]∣∣ ∣∣X1[k]

∣∣

Page 13: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Lengthen by a factor of 2 (N=2048).∣∣X2[k]∣∣ ∣∣X2[k]

∣∣

Page 14: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Lengthen by a factor of 4 (N=4096).∣∣X3[k]∣∣ ∣∣X3[k]

∣∣

Page 15: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Zero Padding

Lengthen by a factor of 8 (N=8192).∣∣X4[k]∣∣ ∣∣X4[k]

∣∣

Peak is now at k = 55 suggesting that analysis for N = 1024 (i.e.,

k = 7) was only approximate. Here we would get k = 558 = 6.875.

Page 16: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Conclusion

Padding with zeros allowed us to compute more samples in fre-

quency.

The greater number of samples better illustrate the shape of the

window function that governs the between-frequency interpolation.

Regardless of the analysis width, resolution of the signal is deter-

mined by a square window of length 1024, since there are only 1024

samples of data available in any of the preceding analyses.

In order to increase frequency resolution, we need to include more

data.

Page 17: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Original (N=1024).

x5[n]∣∣X5[k]

∣∣

Page 18: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 2 (N=2048).

x6[n]∣∣X6[k]

∣∣

Page 19: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 4 (N=4096).

x7[n]∣∣X7[k]

∣∣

Page 20: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 8 (N=8192).

x8[n]∣∣X8[k]

∣∣

Switching again to line plots ...

Page 21: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Original (N=1024).∣∣X5[k]∣∣ ∣∣X5[k]

∣∣

Page 22: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 2 (N=2048).∣∣X6[k]∣∣ ∣∣X6[k]

∣∣

Page 23: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 4 (N=4096).∣∣X7[k]∣∣ ∣∣X7[k]

∣∣

Page 24: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 8 (N=8192).∣∣X8[k]∣∣ ∣∣X8[k]

∣∣

Page 25: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 16 (N=16,384).∣∣X9[k]∣∣ ∣∣X9[k]

∣∣

Page 26: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Longer Sequence

Lengthen by a factor of 32 (N=32,768).∣∣X10[k]∣∣ ∣∣X10[k]

∣∣

Clear peaks at k = 217 and k = 228 (f = 292.04 Hz and f = 306.85 Hz).

Notice that these are the second harmonics of lower frequencies.

The fundamental components were not clearly resolved with

N=1024 but are clear with N=32,768.

Page 27: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Summary: Frequency Resolution

Increasing the length of the analysis increases the number of fre-

quencies that result.

Increasing the length of the analysis by zero padding can better

illustrate the shape of the window function that governs the between-

frequency interpolation.

However, it does not increase frequency resolution.

To increase frequency resolution we must include more data.

Page 28: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Upsampling Time

We can similarly increase time resolution.

Example: Create a wav file to generate a tone at 441 Hz when the

sample frequency is 11,025 samples/second.

Listen to the wav file, and check that it sounds right.

Our goal is to scale the sound so that the new sample frequency

is 44,100 samples/second while preserving all of the same audible

frequency content.

Page 29: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Upsampling Time

Let x[n] represent the sampled signal.

fs = 11025f = 441Omega = 2*pi*f/fsN = 2**14x441 = [cos(Omega*n) for n in range(N)]wav_write(x441,fs,’x441.wav’)

Plot the first 25 samples of the signal.

Plot the magnitude of the DFT of these samples.

Page 30: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Upsampling Time

x441[n]∣∣X441[k]

∣∣

Page 31: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Upsampling Time

To increase frequency resolution, we increased the length of the

time signal (either by padding or including more samples).

To increase time resolution, stretch the time signal (inserting zeros).

Stretch x441[n] by a factor of 4:

y411 = []for n in range(len(x411)):

y411.append(x411[n])y411.append(0)y411.append(0)y411.append(0)

Page 32: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Upsampling Time

Plot the DFT of y[n].

y441[n]∣∣Y 441[k]

∣∣

Now there are 8 components instead of 2. Why?

What are the frequencies of other components?

Page 33: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Upsampling Time

Zero out the unwanted components and take the idft.

z441[n]∣∣Z441[k]

∣∣

Write to wav file and listen. It should sound exactly like the original

but it’s sampled 4 times faster.

Page 34: Using the Discrete Fourier Transform · sample frequency is 11,025 samples/second. Listen to the wav le, and check that it sounds right. Our goal is to scale the sound so that the

Upsampling Time

Try it on an actual piece of audio.

x,fs = wav_read(’flights.wav’)x += (2**15-len(x))*[0]y = []for n in range(len(x)):

y.append(x[n])y.append(0)y.append(0)y.append(0)

Y = fft(y)N = len(x)for k in range(N//2,4*N-N//2+1):

Y[k] = 0z = ifft(Y)wav_write([4*s.real for s in z],4*fs,’fast_flights.wav’)