- This project is classified into 2 milestones: Milestone 1 :You are the PIANIST,you should mix and match frequencies to generate your own song.
Milestone 2 :Noise Cancellation and Frequency Domain (will be posted later after the midterms).
- Single tone generation: Each pressed piano key/note number π, corresponds to a single tone with frequency ππ generated for a certain period of timeππstarting from time π‘π over a defined time range π‘.
π₯π π‘ = sin 2 ππππ‘ [π’(π‘βπ‘π)βπ’(π‘βπ‘πβππ)]
Where the unit step functions defines the playing interval.
Example: Assume that we started pressing the middle Ckey (π 1 = 261. 63 π»π§), at time π‘ 1 = 0 for a duration of π 1 = 1. 5 seconds, the resultant signal is:
π₯ 1 π‘ = sin 2 β 261. 63 ππ‘ [π’(π‘)βπ’(π‘β 0. 5 )]
0 1.5 3
π₯ 1 π‘
- Signal/Song Generation: Generate your own signal using π pairs of notes chosen from the 3 rdand 4 thpiano octaves assuming that you are playing will both hands at the same time ( 3 rdoctave with the left hand/ 4 thoctave with right hand),
Each piano key/note frequency is given below. Your final song could be generated as follows,
π₯ π‘ = ΰ·
π= 1
π
[sin 2 πΟππ‘ +sin 2 ππππ‘ ][π’(π‘βπ‘π)βπ’(π‘βπ‘πβππ)]
Chosen from the 3 rdoctave Chosen from the 4 thoctave
1 ) You will need to import the following libraries.
import numpy as np
import matplotlib.pyplot as plt
import sounddevice as sd
2 ) Set the total song playing time to 3 seconds starting from 0 for 12 Γ 1024
samples.
π‘ =np.linspace( 0 , 3 , 12 β 1024 )
3 ) Customize your own song by setting the number of pairs of notes π.For
each pair number π, set the left hand frequency Οπ, right hand frequency ππ,
the pressing starting time π‘π, and how long you will press both keys ππ.
N.B you can freely customize these values to play the song you want.
If you want to play with one hand only, you can set the frequency of the other hand to 0.
- Procedure:
4 ) Define your signal/song using the previous parameters:
5 ) Plot your signal/song in the time domain your output will look similar to
the following: plt.plot(t, x)
NB. The time separation between the notes will vary from one group to another according to your
song. We also canβt notice the variations in the sinusoidal tones due to the high frequencies.
6 ) Finally:Play your song using:
sd.play(x, 3 β 1024 )
Faculty of Information Engineering and Technology Department of Communication Engineering
- Noise generation: Assume that additional noise is generated by a child who kept pressing the same 2 random piano keys during your song playing interval such that the noise is represented as follows,
Where, ππ 1 and ππ 2 are two frequencies selected randomly from the 3rdand 4th
octaves frequencies list respectively. Your final song after the noise becomes,
Goal: Noise Cancelation: using frequency domain conversion
π π‘ =sin 2 ππ 1 ππ‘ +sin 2 ππ 2 ππ‘
0) You will need to add this line at the beginning of your code:
from scipy.fftpackimport fft
1) Set the number of samples π to π¦ππ’π_π πππ_ππ’πππ‘πππx 1024. Then set
the frequency axis range to,
π = 3 x 1024
π= np.linspace( 0 , 512 ,int(π/ 2 ))
2) Convert the time signal π₯(π‘)to the frequency signal π(π) from,
Your final output will look as following,
- Procedure:
- Procedure:
3) Generate the noise signal as described in slide 3 where the two random
frequencies are selected as follows,
Hint: Each time you run the code you will get different noise.
4) Add the generated noise π(π‘) to the signal π₯ π‘ ,
π₯π π‘ = π₯ π‘ +π(π‘)
Hint: Play the new signal to notice the noise effect on the sound.
5) Convert the noise contaminated signal π₯π π‘ to the signal ππ π in
frequency domain, it looks as follows,
Hint: You will notice that the signal contains two very high frequency peaks
- Procedure: 6) Find the two random noise frequencies:by finding the frequency indices that corresponds to the peaks of the signal ππ π which is higher than the maximum peak of the original signal π π without noise rounded to the next integer (to avoid including the maximum value due to double precision errors).
7) Round the frequency values at that indices in π to get παπ 1 and παπ 2 (since
we generate random integer values of frequencies)
8) Filter the noise by subtracting two tones with the two found frequencies
π₯ππππ‘ππππ π‘ =π₯π π‘ β[sin 2 ππ 1 ππ‘ +sin 2 ππ 2 ππ‘ ]
9) Play the filtered signal to make sure that you get the same original sound
without noise
sd.play(π₯ππππ‘ππππ, 4 β 1024 )