# Exercises

# 7.4. Exercises#

Synthesize a sinusoid \(x[n] = A \cdot \cos(2\pi \cdot f \cdot n / N + \phi)\) by directly constructing the frequency domain representation \(X[m]\) and applying the inverse DFT. Use the following starter code:

```
# sampling rate
fs = 8000
# duration / number of frequencies
N = 8000
# Wave parameters
A = 5
f = 250 # Hz
phi = np.pi / 4
X = np.zeros(N, dtype=np.complex)
# INSERT YOUR CODE HERE TO BUILD X
# Invert to recover the signal
x = np.fft.ifft(X)
```

Hint

Check your synthesis against a signal generated purely in the time-domain. Your IDFT should be numerically identical to the time-domain signal, including amplitude and phase.

Make sure to construct your DFT so that the output is real-valued.

A *low-pass filter* allows frequencies below some cut-off \(f_T\) to propagate through, and blocks all frequencies above the cut-off. Try implementing this on a signal of your choice by setting any DFT coefficients \(X[m]\) to 0 if the corresponding frequency is above 500 Hz. Apply the inverse DFT and listen to the results. How does it sound? Do you notice any artifacts?

Hint

It is best to try this on a few different sounds, including natural and synthetic signals. Hand claps might be particularly interesting.

Using a signal `x`

of your choice (ideally a single pitched note or isolated sound), compute its DFT:

```
X = np.fft.fft(x)
```

and the following reconstructions:

The inverse DFT:

`x_inv = np.fft.ifft(X)`

.The inverse DFT with the original magnitudes and all phases set to 0:

`x_nophase = np.fft.ifft(np.abs(X))`

.The inverse DFT with the original phases and all magnitudes set to 1:

`x_nomag = np.fft.ifft(np.exp(1j * np.angle(X)))`

.

Then, answer the following questions:

How do these three signals compare to each other?

How do they compare to the original signal?

What does this tell you about the relative importance of magnitude and phase?

How might your answers change with a different kind of test signal?