import matplotlib.pyplot as plt
from math import pi,cos,sin
from lib6003.audio import wav_write, wav_play
##########
# Part 1 #
##########
# Make a WAV file containing 3 seconds of a 440Hz pure sine tone.
f = 440
fs = 44100
seconds = 3
Omega = 0 #TODO: what should this value be?
samples = []
for n in range(0): # TODO: how many samples should we generate?
samples.append(sin(Omega*n))
wav_write(samples,fs,'440.wav')
## plot the first 150 samples using matplotlib
#plt.plot(samples[:150])
#plt.show()
##########
# Part 2 #
##########
# Start by generating one period of a discrete cosine with a period of 10
# samples.
x1 = []
for n in range(10):
x1.append(cos(0)) # TODO: what should the argument to cos() be?
# Check by making a plot
import matplotlib.pyplot as plt
plt.stem(x1)
plt.show()
# Extend the list to 10 cycles.
x10 = x1 * 10
plt.stem(x10)
plt.show()
# Make a .wav file to play this list at 2000 samples per second.
# Predict: What frequency should we hear?
# Predict: if we use 200 cycles of the wav (as below), what is the duration of
# this sound?
wav_write(x1*200, 2000, 'mystery.wav')
# Is it possible to modify the sampling rate (not the frequency Omega) to make
# a 400Hz tone? 800Hz? 4800Hz? If so, what values of the sampling rate should we use?
# Compute a second list of numbers with different discrete frequency.
# Play that list so that we hear 200 Hz.
Omega = 0 # TODO
fs = 0 # TODO
x2 = [cos(Omega*n) for n in range(20)]
wav_write(x2, fs, '200Hz_2.wav')
##########
# Part 3 #
##########
# Using a sample rate of 8kHz, create 1-second .wav files
# of cosines with the following frequencies (in kHz):
# 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
fs = 8000
for i in range(1,16):
f = i*1000
c_samples = [cos(2*pi*f/fs * n) for n in range(fs)]
wav_write(c_samples, fs, 'cos_%02d.wav' % i)
# Try also with sines
fs = 8000
for i in range(1,16):
f = i*1000
s_samples = [sin(2*pi*f/fs * n) for n in range(fs)]
wav_write(s_samples, fs, 'sin_%02d.wav' % i)
# What do we hear, and why?
##########
# Part 4 #
##########
# compare signals
x1 = [cos(0.2*pi*n) for n in range(20)]
x2 = [cos(2.2*pi*n) for n in range(20)]
x3 = [cos(1.8*pi*n) for n in range(20)]
plt.stem(x1)
#plt.stem(x2)
#plt.stem(x3)
plt.show()