Sinusoidal Waves

  • $f(x) = A\sin(\omega n Ts + \phi) = A\sin(2 \pi f n Ts + \phi)$, where $\omega = 2 \pi f$
    • $\omega$ = angular freq,
    • n = time index
    • T = 1.0/Fs, the sampling freq.

In [21]:
import numpy as np
import matplotlib.pyplot as plt
import IPython

In [19]:
A = .8
f = 1000
Fs = 44100
phi = np.pi/2
n = np.arange(-0.002,0.002,1.0/Fs)
x = 2 * np.sin(2*np.pi*f*n + phi)

In [20]:
plt.plot(n,x)
plt.title("Sinusoidal Signal")
plt.xlabel(r'time $\rightarrow$')
plt.ylabel(r'Amplitude$\rightarrow$')
plt.show()


Reading and writing audio files


In [32]:
import numpy as np
import scipy.io.wavfile as wv
import matplotlib.pyplot as plt
from IPython.display import Audio

In [41]:
path = '/home/pratyush/workspace/python/audio/sms-tools/sounds/'
fileName = 'flute-A4.wav'

try:
    # Read the file
    [Fs, data] = wv.read( path + fileName )
    
    # Plot the data
    plt.subplot(221)
    plt.plot(data, 'b')
        
    # Plot with time axis
    t = np.arange(0,data.size)/Fs
    plt.subplot(222)
    plt.plot(t,data,'b')
    plt.xlabel(r'time $\rightarrow$')
    plt.ylabel(r'Amplitude $\rightarrow$')
    plt.title('Audio File Plot')
        
    # Plot a portion of the signal
    x = data[Fs:Fs+500]
    t1 = np.arange(Fs,Fs+x.size)/Fs
    plt.subplot(223)
    plt.plot(t1,x,'b')
    plt.xlabel(r'time $\rightarrow$')
    plt.ylabel(r'Amplitude $\rightarrow$')
    plt.title('Portion of the audio file')
    
    plt.tight_layout()
    plt.show()
    
    # Save the data to a file
    newFileName = 'flute-A4_test.wav'
    wv.write(data=data, filename=path+newFileName, rate=int(2*Fs))
        
except FileNotFoundError as err:
    print(err)



In [39]:
# Play the files
Audio(path+fileName)


Out[39]:

In [42]:
# Play the second file
Audio(path+newFileName)


Out[42]: