from IPython.core.display import HTML
def set_width(width):
    display(HTML(f"""<style>  
            .container {{ width:{width}% !important; 
                            min-width:800px !important; margin: 0 auto}} 
            .jp-Cell {{ width:{width}% !important; 
                            min-width:800px !important; margin: 0 auto}} </style>"""))
# Set container width to X% of the fullscreen 
set_width(50)

8C: Aliasing in combined sines#

Learning goal: be able to calculate the outcome of aliasing in case of combined sines. Understand how each individual sine contributes to the result.

Structure of an experiment:

  • Background + Anticipate + Stimulate (45 min): per person. This is homework and should be finished before you start your 4 hours practicum session

  • Implement + Investigate (50 min): with your partner(group of 2)

  • Compare + Conclude (10 min): with a group of 4(per table)

This notebook does not need an alpaca nor the classroom equipment

BACKGROUND#

⏳ Estimated time: 15 min

You already saw that the phase of the recorded signal might be in antiphase of the original signal due to aliasing. The phase depends on whether \( f+ 2N f_{Nyquist}\) is below or above \(f_{Nyquist}\). When observing the Fourier transform, we often look at the absolute value and neglect the phase. For a single sine the phase is not that interesting, but it is different when acquiring a combined signal, consisting of two sines with different frequency.

When recording two sines, one or both sines might become aliased. In that case the phase does matter, if the two frequencies (aliased or not) are measured at the same measured frequency. It helps to remember that a sine in antiphase can be seen as a sine with a negative amplitude, and that in case of destructive interference, the sum of two signals might be 0 instead of 2:

\(sin(2 \pi f_0) +sin (2 \pi f_0 + \pi) = sin(2 \pi f_0) -sin (2 \pi f_0)=0\)

ANTICIPATE: what would be the outcome of different 2 sines aliasing#

⏳ Estimated time: 15 min

An aliased signal can be in antiphase of its original signal. Now, imagine two sines with different frequencies and equal amplitude (say value 1) are recorded, and the sampling rate will be varied. Brainstorm + write down all possible outcomes (in terms of overal number of peaks, which signal is aliased, and height of peaks) for the Fourier transform/ frequency spectrum.

ℹ️ Hint
you should be able to get a variation of number of peaks (0,1,2), similarly you should also get a variation of amplitudes (0,1,2)

### TO DO ="your answer to all possible outcomes of the sum of two (un)aliased sines"

SIMULATE: outcome of aliasing 2 sines#

⏳ Estimated time: 15 min

Now you will try to verify your predictions using calculate_measured_frequency you created in 8B. Say you have two signals with amplitude 1, frequencies 300 and 650 Hz. Find two values of the sample rate, for which the measured signal will give:

  1. no peaks at all

  2. one peak with amplitude 2

Feel free (not obligated) to adapt the function to also output the phase/antiphase.

### TO DO="your calculate_measured_frequency and calculate_measured_frequency_phase:" 

def calculate_measured_frequency( signal_frequency, sampling_rate):

    remainder = signal_frequency%sampling_rate
    if remainder < sampling_rate/2:
        return remainder 
    if remainder > sampling_rate/2:
        return sampling_rate - remainder
    return 0

def calculate_measured_frequency_phase( signal_frequency, sampling_rate):

    remainder = signal_frequency%sampling_rate
    if remainder < sampling_rate/2:
        return remainder , 1
    if remainder > sampling_rate/2:
        return sampling_rate - remainder , -1
    return 0, 0

# one aliased, one not:
print(calculate_measured_frequency_phase(650, 950))
# how to calculate: find the frequency exactly between 300 and 650 --> fN=650/2=475 Hz, fs=950

# both aliased, one in phase, one in antiphase
print(calculate_measured_frequency_phase(650, 2*237.5))
print(calculate_measured_frequency_phase(300, 2*237.5))
# how to calculate: aliased 300=> fN-(300-fN)= 2 fN-300; aliased 650=> 650-2fN. When put equal, fs= 2fN=475 Hz, fN=237.50

# both aliased, measured as 0
print(calculate_measured_frequency_phase(650, 50))
print(calculate_measured_frequency_phase(300, 50))
# how to calculate: find a value which fits N times in 300 and 650
### END SOLUTION
(300, -1)
(175.0, 1)
(175.0, -1)
(0, 1)
(0, 1)
### TO DO ="your answer, adapt your prediction if needed"

Feel free to watch the precap from last year:

IMPLEMENT & INVESTIGATE 1: observing superposition#

⏳ Estimated time: 30 min

You have learned how aliasing works for a single sine. You also learned how to determine the phase of the aliased signal: whether it is inverted or not. In this exercise, you will learn to apply this knowledge to multiple sines.

When an electronic circuit contains more than one source, we can often (in case of a linear time-invariant system) add the two individual voltages to calculate the combined result. This is the principle of superposition. Let’s have a look at the superposition of nine sines with the frequencies 1 Hz, 2 Hz, 3 Hz, …, 9 Hz.

\[ y(t) = A_1 \sin(1\cdot 2\pi t) + A_2 \sin(2\cdot 2\pi t) + A_3 \sin(3\cdot 2\pi t)+\cdots + A_9 \sin(9\cdot 2\pi t) \]

Run the code below. You may need to run it twice before the plot appears.

import ipywidgets as widgets
import numpy as np
import matplotlib.pyplot as plt

time = np.arange(0.0, 3.0, 0.01)

def overtones(amp1, amp2, amp3, amp4, amp5, amp6, amp7, amp8, amp9):
    signal =  amp1*np.sin( 1 * 2*np.pi*time)
    signal += amp2*np.sin( 2 * 2*np.pi*time)
    signal += amp3*np.sin( 3 * 2*np.pi*time)
    signal += amp4*np.sin( 4 * 2*np.pi*time)
    signal += amp5*np.sin( 5 * 2*np.pi*time)
    signal += amp6*np.sin( 6 * 2*np.pi*time)        
    signal += amp7*np.sin( 7 * 2*np.pi*time)
    signal += amp8*np.sin( 8 * 2*np.pi*time)
    signal += amp9*np.sin( 9 * 2*np.pi*time)    
    
    plt.plot(time, signal )
    plt.show()

widgets.interact(overtones, amp1=widgets.FloatSlider(min=-2.5, max=2.5, step=0.25, value=1, description='Amp 1:'),
         amp2=widgets.FloatSlider(min=-1.25, max=1.25, step=0.125, value=0, description='Amp 2:'),
         amp3=widgets.FloatSlider(min=-0.8333333333, max=0.8333333333, step=0.08333333333, value=0.33333, description='Amp 3:'),
         amp4=widgets.FloatSlider(min=-0.625, max=0.625, step=0.0625, value=0, description='Amp 4:'),
         amp5=widgets.FloatSlider(min=-0.5, max=0.5, step=0.05, value=0.2, description='Amp 5:'),
         amp6=widgets.FloatSlider(min=-0.416665, max=0.416665, step=0.0416665, value=0, description='Amp 6:'),
         amp7=widgets.FloatSlider(min=-0.35714, max=0.35714, step=0.035714, value=0, description='Amp 7:'),
         amp8=widgets.FloatSlider(min=-0.3125, max=0.3125, step=0.03125, value=0, description='Amp 8:'),
         amp9=widgets.FloatSlider(min=-0.27778, max=0.27778, step=0.027778, value=0, description='Amp 9:'))
<function __main__.overtones(amp1, amp2, amp3, amp4, amp5, amp6, amp7, amp8, amp9)>

Experiment with different amplitudes, so that you understand what superposition is. It might be helpful to first put all amplitudes to 0 except first and second.

ℹ️ Hint
You can either use the slider, or left click on the numerical value, to adapt it.

Set Amp 1 to \(1\), set Amp 2 to \(\frac{1}{2}\), set Amp 3 to \(\frac{1}{3}\) et cetera. (a) What pattern do you approach? (b) Draw the frequency spectrum of this pattern and upload a picture.

### TO DO=" Write your answer down here; which patteren do you see?"
from ipywidgets import FileUpload
from IPython.display import Image
import os
upload=FileUpload()
upload
file_name="8C_Spectrum_1.jpg"
if upload.value!={}:
    with open(file_name,"wb") as f:
        try: f.write(upload.data[-1]) # python 3.7 Kernel code
        except: f.write(upload.value[-1]["content"])  # python 3.8 Kernel code

Image(filename=file_name, width="50%")

Set all even amplitudes to zero. (a) What pattern do you approach now? (b) Draw the frequency spectrum of this pattern.

### TO DO="write your answer here: which pattern do you see?"
#b. upload the spectrum
upload
file_name="8C_Spectrum_odd_2.jpg"
if upload.value!={}:
    with open(file_name,"wb") as f:
        try: f.write(upload.data[-1]) # python 3.7 Kernel code
        except: f.write(upload.value[-1]["content"])  # python 3.8 Kernel code

Image(filename=file_name, width="50%")

From now on, we will consider the signal with odd frequencies up to 19 Hz, where Amp 1 is \(2\), Amp 3 is \(\frac{2}{3}\), Amp 5 is \(\frac{2}{5}\), Amp 7 is \(\frac{2}{7}\) et cetera. In formula form, that is:

\[ y(t) = \sum_{k=1, k\text{ odd}}^{19} \frac{2}{k} \sin( k \cdot 2\pi t)) \]

You know from the previous exercises that this signal approximates a square wave.

Imagine that we sample this signal with a sampling rate of 19 Hz. Use your knowledge of aliasing to draw the frequency spectrum of the aliased signal. Upload the drawing

ℹ️ Hint If the sampling rate is 19 Hz, what is the Nyquist frequency? Which frequencies will be aliased? For example, what is the measured frequency of input frequency 11 Hz?

You should be able to do the calculation as back-of-the-envelope, but may use the interactive figure or the calculate_measured_frequency() to verify your calculations

### TO DO="your answer for the Nyquist frequency when sampling with 19 Hz, which frequencies aliase, how will a 11 Hz be measured?"
# upload your drawing

upload
file_name="8C_Spectrum_odd_3.jpg"
if upload.value!={}:
    with open(file_name,"wb") as f:
        try: f.write(upload.data[-1]) # python 3.7 Kernel code
        except: f.write(upload.value[-1]["content"])  # python 3.8 Kernel code

Image(filename=file_name, width="50%")

IMPLEMENT & INVESTIGATE 2: observing interference#

⏳ Estimated time: 20 min

Suppose that we have a single sine of variable frequency \(f\). We measure it with a sampling frequency of 50 Hz.

What frequency would you measure with this sampling frequency

  1. if \(f=10\text{Hz}\),

  2. if \(f=20\text{Hz}\),

  3. if \(f=30\text{Hz}\)?

### TO DO ="your 'measured' frequencies"

Now, we will put these three signals in superposition, like so:

\[ y(t) = \sin(10\cdot2\pi t)+\sin(20\cdot2\pi t)+\sin(30\cdot2\pi t) \]

Predict what its frequency spectrum woud look like when sampled with a rate of 50 Hz. If you find it hard, try drawing the result.

### TO DO="your prediction for the superposed, aliased signal"

In the frequency spectrum, you might have predicted two peaks since the 30 Hz signal lands on the 20 Hz. However, the answer looks like this. Red is the analogue signal. Blue is the aliased signal. The samples are green.

Explain why there is only one peak in this signal.

ℹ️ Hint Use words like antiphase in your explanation

### TO DO="explanation on why you see only one peak"

Suppose that we increase the 30 Hz frequency to 70 Hz, like so:

\[ y(t) = \sin(10\cdot2\pi t)+\sin(20\cdot2\pi t)+\sin(70\cdot2\pi t) \]

Now, the last sine will land on 20 Hz too, but not in anti-phase.

Predict how high the peak in the aliased frequency spectrum will be at 20 Hz?

### TO DO="your prediction on the peak height"

For the following signal, draw the frequency spectrum after sampling with a rate of 20 Hz. Indicate the heights of the peaks.

\[ g(t) = 1 -3\sin(2\pi t) + 2\sin(28\cdot 2\pi t) + \sin(12\cdot 2\pi t) + \sin(32\cdot 2\pi t) + 3\sin(21\cdot 2\pi t) \]
### TO DO="indicate the location and height of the peak(s)"
upload
file_name="8C_Interference_4.jpg"
if upload.value!={}:
    with open(file_name,"wb") as f:
        try: f.write(upload.data[-1]) # python 3.7 Kernel code
        except: f.write(upload.value[-1]["content"])  # python 3.8 Kernel code

Image(filename=file_name, width="50%")

Optional challenge
the Siglent function generator has two output channels, so you can generate two signals, and record it a certain sample rate. For example select a sine of 350 Hz and 850 Hz. Predict and verify experimentally at which sampling rate the two signals destructively and constructively add.

COMPARE & CONCLUDE#

⏳ Estimated time: 10 min

  • Wait till all (4) group members finish their observation

  • Compare your results with your other group members.

  • If your results agree, and are in line with all predictions, then talk to a TA and get checked off

  • Otherwise, so if your results do not agree, or your results are not in line with your predictions, then first discuss amongst your group before getting a TA.

to be checked off by a TA:

  1. Discuss the answer about the frequency spectrum of (g(t)) (the last question)

  2. Explain how different combinations of interference can lead to a. 0, 1 or 2 peaks, b. amplitudes between 0 and 2?

  3. exit card:

    1. Write a brief abstract on what you learned (conclusion, useful graph),

    2. Which troubleshooting skills do you want to remember for next sessions,

    3. Which code do you copy for use in next sessions,

  4. How do you think this notebook could be improved

#8C aliasing multiple sines
### TO DO='1. Discuss the answer about the frequency spectrum of (g(t))'

### TO DO='2. explain how aliasing and interference can lead to a. peaks 0-2, b. amplitude 0-2

### TO DO="3a. abstract"

### TO DO="3b. troubleshooting"

### TO DO="3c. code"

### TO DO="4. what changes would you suggest?"