<jupyter_start><jupyter_text>Sampling[<NAME>](http://control.ucsd.edu/mauricio)*September 2019*In this notebook you will generate waveforms and play them as audio files.We will explore different sampling rates as well as mono versus stereo files. Load some packages<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import math
import librosa<jupyter_output><empty_output><jupyter_text>Generate some tonesLet's start by generating a time base vector for T = 2s waveforms. We will do that at two sampling rates: fs1 = 8000Hz, t1, and fs2 = 16000Hz, t2.<jupyter_code>T = 2.0

fs1 = 8000
t_8000 = np.arange(0., T, 1/fs1)

fs2 = 16000
t_16000 = np.arange(0., T, 1/fs2)<jupyter_output><empty_output><jupyter_text>We will use these vectors to generate two tones, one at f1 = 440 Hz, x1, and another at f2 = 446 Hz, x2.<jupyter_code>f1 = 440.0
A1 = .8
phi1 = 0

f2 = 446
A2 = .8
phi2 = np.pi*90/180<jupyter_output><empty_output><jupyter_text>First at fs = 8000Hz<jupyter_code>x1_8000 = A1*np.sin(2*np.pi*f1*t_8000+phi1)
x2_8000 = A2*np.sin(2*np.pi*f2*t_8000+phi2)<jupyter_output><empty_output><jupyter_text>Then at fs = 16000Hz<jupyter_code>x1_16000 = A1*np.sin(2*np.pi*f1*t_16000+phi1)
x2_16000 = A2*np.sin(2*np.pi*f2*t_16000+phi2)<jupyter_output><empty_output><jupyter_text>Let's plot to visualize the waveforms at 8000Hz<jupyter_code>plt.plot(t_8000, x1_8000, '-o', t_8000, x2_8000, '-o')
plt.title('Sine Waves, f1 = {0}Hz, f2 = {1}Hz, Sampling Rate = {2}Hz'.format(f1,f2,fs1));
plt.xlabel('t (s)');
plt.ylabel('x1(t) and x2(t)');
plt.xlim(1,1.01)
plt.grid(1)
plt.show()<jupyter_output><empty_output><jupyter_text>Compare the waveforms sampled at 8000Hz versus 16000Hz<jupyter_code>plt.plot(t_8000, x1_8000, '-o', t_16000, x2_16000, '-o')
plt.title('Sine Waves, f1 = {0}Hz, Sampling Rate = {1}Hz, f2 = {2}Hz, Sampling Rate = {3}Hz'.format(f1,fs1,f2,fs2));
plt.xlabel('t (s)');
plt.ylabel('x1(t) and x2(t)');
plt.xlim(1,1.01)
plt.grid(1)
plt.show()<jupyter_output><empty_output><jupyter_text>Play the tonesWe will now play these tones<jupyter_code>import IPython.display as ipd<jupyter_output><empty_output><jupyter_text>First at fs = 8000Hz<jupyter_code>ipd.Audio(x1_8000, rate=fs1)
ipd.Audio(x2_8000, rate=fs1)<jupyter_output><empty_output><jupyter_text>Then at fs = 16000Hz<jupyter_code>ipd.Audio(x1_16000, rate=fs2)
ipd.Audio(x2_16000, rate=fs2)<jupyter_output><empty_output><jupyter_text>What if we make a mistake on the sampling rates?<jupyter_code>ipd.Audio(x1_8000, rate=fs2)
ipd.Audio(x1_16000, rate=fs1)<jupyter_output><empty_output><jupyter_text>Combining tonesLet's us first combine the two tones by summing them up. In this part we will work only with signals at fs = 8000Hz<jupyter_code>x12_8000 = (x1_8000 + x2_8000)/2
plt.plot(t_8000, x12_8000, '-')
plt.title('Combined Sine Waves, f1 = {0}Hz, f2 = {1}Hz, Sampling Rate = {2}Hz'.format(f1,f2,fs1));
plt.xlabel('t (s)');
plt.ylabel('x1(t) and x2(t)');
plt.xlim(0.041,0.041+1./6)
plt.grid(1)
plt.show()<jupyter_output><empty_output><jupyter_text>Do you *see* the beat?<jupyter_code>ipd.Audio(x12_8000, rate=fs1)<jupyter_output><empty_output><jupyter_text>Let's us now combine the two tones by placing each of them in one channel of a stereo signal.<jupyter_code>x12_8000s = np.vstack((x1_8000,x2_8000))
ipd.Audio(x12_8000s, rate=fs1)<jupyter_output><empty_output><jupyter_text>Listen to the above file on headphones. Do you *hear* a beat?  Working with an audio fileLet's us repeat this exercise with a music file.Upload your favorite song by dragging it and dropping on the notebook file manager, then change the name below to reflect the filename of the song.We will only load 20s of the song to make things faster.<jupyter_code>x1, fs = librosa.load('Stevie Wonder Superstition.mp3', mono=False, offset=20, duration=20)<jupyter_output><empty_output><jupyter_text>Visualize the samples that make up your song:<jupyter_code>plt.plot(x1[:,11*fs:11*fs+round(.1* fs)].T, '.')
plt.title('Sampling Rate = {0}Hz, Left and right channels'.format(fs));
plt.xlabel('t (s)');
plt.ylabel('x1(t)');
plt.grid(1)
plt.show()<jupyter_output><empty_output><jupyter_text>and play the song<jupyter_code>ipd.Audio(x1, rate=fs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14156769596199525, 'fraction_numerical': 0.08859857482185274, 'mean_word_length': 7.036259541984733, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '407133', 'license': 'MIT', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1742, 'n_words': 382, 'path': 'SignalsAndSystems/Sampling.ipynb', 'repo_name': 'mcdeoliveira/Notebooks', 'size': '4224'}