For reference, here is the list of frequencies of all 6 strings expected for a well tuned guitar:
String | Frequency | Scientific pitch notation |
---|---|---|
1 (E) | 329.63 Hz | E4 |
2 (B) | 246.94 Hz | B3 |
3 (G) | 196.00 Hz | G3 |
4 (D) | 146.83 Hz | D3 |
5 (A) | 110.00 Hz | A2 |
6 (E) | 82.41 Hz | E2 |
In [79]:
import sounddevice as sd
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
sd.query_devices()
Out[3]:
In [71]:
device = 0 # we use my USB sound card device
duration = 2 # seconds
fs = 44100 # samples by second
In [72]:
myrecording = sd.rec(duration * fs, samplerate=fs, channels=1, device=0)
In [80]:
df = pd.DataFrame(myrecording)
df.loc[25000:30000].plot()
Out[80]:
In [39]:
fourier = np.fft.fft(rec)
We can visualise a section of the Fourier transform to notice there is a clear fundamental frequency:
In [88]:
plt.plot(abs(fourier[:len(fourier)/10]))
Out[88]:
In [89]:
f_max_index = np.argmax(abs(fourier[:fourier.size/2]))
freqs = np.fft.fftfreq(len(fourier))
freqs[f_max_index]*fs
Out[89]:
In [90]:
rec = myrecording.ravel()
rec = rec[25000:30000]
autocorr = np.correlate(rec, rec, mode='same')
plt.plot(autocorr)
Out[90]:
In [ ]: