For simplicity I am using the Anaconda distribution on my Macbook Pro for this notebook.

The purpose is to first experiment here with sample WAV files. Each file comes from a database of free samples provided free of rights by the Philharmonia Orchestra at http://www.philharmonia.co.uk/explore/sound_samples/.

We will use 6 samples representing a long Forte string pick of each of the 6 strings of an accoustic guitar tuned in Standard E.

Note: I have converted the sample files myself from their original mp3 format to wav format with 32bit, 44100Hz and mono channel.

We will use two different methods for detecting the pitch and compare their results.

For reference, here is the list of frequencies of all 6 strings expected for a well tuned guitar:

String | Frequency | Scientific pitch notation | Sample |
---|---|---|---|

1 (E) | 329.63 Hz | E4 | Sample file |

2 (B) | 246.94 Hz | B3 | Sample file |

3 (G) | 196.00 Hz | G3 | Sample file |

4 (D) | 146.83 Hz | D3 | Sample file |

5 (A) | 110.00 Hz | A2 | Sample file |

6 (E) | 82.41 Hz | E2 | Sample file |

```
In [1]:
```import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

```
```

**We will use scipy from the Anaconda distribution to read the WAV sample files**

```
In [24]:
```from scipy.io import wavfile
# Let's start with the first sample corresponding to the lower string E2
rate, myrecording = wavfile.read("samples/guitar_E2_very-long_forte_normal.wav")
print(rate, np_array.size)

```
```

```
In [12]:
```duration = 1 # seconds
fs = rate # samples by second

```
In [33]:
```# Let's restrict our sample to 1 second of the recording, after 0.5 second of sound to avoid the string picking
array = myrecording[int(0.5*fs):int(2.5*fs)]
print(array.size)

```
```

**Let's plot a section of this array to look at it first**

We notice a pretty periodic signal with a clear fundamental frequency: which makes sense since a guitar string vibrates producing an almost purely sinuzoidal wave

```
In [34]:
```df = pd.DataFrame(array)
df.loc[25000:35000].plot()

```
Out[34]:
```

```
In [35]:
```fourier = np.fft.fft(array)

```
In [36]:
```plt.plot(abs(fourier[:len(fourier)/10]))

```
Out[36]:
```

```
In [37]:
```f_max_index = np.argmax(abs(fourier[:fourier.size/2]))
freqs = np.fft.fftfreq(len(fourier))
freqs[f_max_index]*fs

```
Out[37]:
```

**This method detects a fundamental frequency of 248Hz, which is wrong.**

```
In [51]:
``````
# Work in progress: coming soon
```

```
In [50]:
```rec = array
rec = rec[15000:35000]
autocorr = np.correlate(rec, rec, mode='same')
plt.plot(autocorr)

```
Out[50]:
```

```
In [ ]:
```