# 0.5 Amplitude Modulation

### Using Python Functions to Perform Operations

Now, let's write a simple function to multiply two signals. In particular, we will multiply the speech signal $$s\[n]$$ with a cosine signal $$\cos(\omega\_0 n)$$. We will use the `multiply` function from NumPy.

```python
def multiply_signals(x, omega, Fs):
    w = (float(omega) / Fs)
    return np.multiply(x, np.cos(w * np.arange(0,len(x))))
```

Possible explorations are to try other operations and other functions. See NumPy [documentation](https://numpy.org/doc/stable/) for more. We will use `multiply_signal` on the speech signal.

{% hint style="info" %}
*What is the expected outcome? Can you guess? Better, can you analytically show the outcome?*
{% endhint %}

```python
alien_voice = multiply_signals(s, 3000, Fs)
IPython.display.Audio(alien_voice, rate=Fs)
```

Let's be sure to save the result. Don't want to miss out on hearing aliens again!

```python
wavfile.write('alien.wav', Fs, alien_voice)
plot_signal(alien_voice, xaxis_label=r'$n$',
    yaxis_label=r'$s[n]\cos(\omega_0n)$', save='output')
```

{% hint style="success" %}

### Checkpoint

* *Can you notice differences in the signals?*
* *If yes, and explain why the speech signals sound different based on the observations.*
* *If no, comment why you think the speech signals sound different.*
  {% endhint %}

Let's see the answers in the Fourier domain. We will write a function to plot the spectrum of the signal using the fft function.

```python
def plot_spec(x, Fs, max_freq=None, do_fft=True):
    C = int(len(x) / 2)
    if max_freq:
        C = int(C * max_freq / float(Fs) * 2) 
    X = np.abs(np.fft.fft(x)[0:C]) if do_fft else x[0:C]
    N = Fs * np.arange(0, C) / len(x);
    plt.plot(N, X)
    plt.xlabel(r'$\gamma$')
    plt.ylabel(r'$\hat{x}(\gamma)$')
    return N, X
    
    plot_spec(s, Fs, Fs/2)
```

Model the speech as a continuous-time signal $$x(t)$$, and consider the carrier signal $$\cos(2\pi f\_0 t)$$. The output of amplitude modulation using the cosine carrier $$x(t)\cos(2\pi f\_0 t)$$ will have the Fourier transform:

$$
\frac{1}{2}\left\[\hat{x}(\gamma+f\_0) + \hat{x}(\gamma-f\_0)\right],
$$

achieving shifting up of the spectrum.

```python
plot_spec(multiply_signals(s, 3000, Fs), Fs, Fs/2);
```

Note that the spectrum of the amplitude modulated signal moves up to around 500 Hz. Also, since the implementation is in discrete time, the spectrum also undergoes aliasing. All these effects are observed in the audio file.
