Guitar Tuner Using MATLAB

by claraalbndn in Circuits > Software

45 Views, 0 Favorites, 0 Comments

Guitar Tuner Using MATLAB

GUITAR TUNER USING MATLAB APP DESINGER.png

This guitar tuner is based on the tempered system and uses simple MATLAB functions.

I will link a video and a user guide to use this tunner.

Disclaimer: all code coments and the user interface are in Spanish.

Supplies

All I used was MATLAB app desinger and the audio toolbox, and a spanish guitar.

Calculate the Frecuencies

To calculate the frequencies of the guitar strings, I used the equal temperament system starting from A4 at 440 Hz, assuming that one semitone equals to root 12 of 2.

  1. First string, E4 : 329.63Hz
  2. Second string, B3: 246.94 Hz
  3. Third string, G3: 196 Hz
  4. Forth string, D3 : 146.83 Hz
  5. Fifth string, A2: 110 Hz
  6. Sixth string, E2: 82.41 Hz

For detecting the tuning I have used a tolerance interval of 1Hz. And for detecting the strings I hav stablished this detecction intervals:

  1. First string, E4 : 310-350 Hz
  2. Second string, B3: 230-260 Hz
  3. Third string, G3: 180-210 Hz
  4. Forth string, D3 : 140-160 Hz
  5. Fifth string, A2: 100 -120Hz
  6. Sixth string, E2: 70-90 Hz


Before the Coding

interfaz.png

This app is made in the MATLAB App Desinger. The first thing to do is knowing how it is going to work.

I thought the easiest way to do it is by three steps:

  1. Recording the string sound.
  2. Processing that audio.
  3. Give feedback to the user.

From that point it's just desinging the grafic interface of the app desinger. I desinged a very basic user interface because I wanted it to be very intuitve. In this interface we have axes to view the spectrum of the string, a semicicurlar gauge for the tunning and a button to start recording.

There are also three labels to display other information such as the frecuency detected, the note, and the tunning of the sting.


Recording the String

callback boton.png

For this task I decided it was going be activated by a button so the code for the recording is in the callback function of the button.

But before coding the callback function we need to define the properties (which are global viariable). I only used two:

Once we have this defined we can start coding.

For recording the sound three functions are needed. This function are from the MATLAB audio toolbox.

audiorecorder (8000, 16,1) : record the sound at a 8000 kHz sample rate, using 16 bit and 1 channel.

recordblocking(app.grabCuerda, 5) : holds the program and stop the recording after 5 seconds.

grabacion= getaudiodata(app.grabCuerda) : this line is used to extract the audio from the property object and place it in a normal MATLAB variable.

In this specific program recordblocking could be replaced by record, but because it is going to be removed from the MATLAB library, I decided not to use it.

While recording, the value of estadoGrabacion is switched to true. This is made because I don't want the program starting all over again if the button if doubled clicked. After the recording is finished estadoGrabacion is switched back to false and the function procesado (where the audio is processed) is called.


Function Procesado I

procesado1.1 ssi.png
Captura de pantalla 2024-12-07 141543.png

In this function the audio is processed and analysed to provide the information of the tunning, this information is also displayed in the user interface.

In the first secction of the code, the main frecuency played by the guitar is exctracted. In the fisrt photo you can see that first of all I defined the sample rate at 8 kHz. After that the Fast Fourier Transfer Function is done to the signal and then it is represented in the axes. For the representation the X axis is limited from 0 to 400 Hz, this way we are going to visualise the frecuency range of the guitarsĀ“strings.

For extracting the main frecuency, a range of valid frecuencies is defined (from 50 to 350) this way we exclude all the higher harmonic frequencies that we don't need. In the variables rango_minimo and rango_maximo are saved this values. From here, we define the interval we are going to work with and then extract the frecuency using the function max.

Later on, while I testing the code, I realised that this code didn't work with the lower string (E2), this is because the majority of computers have a filter in these frecuencies to avoid problems with the electrical network. So i had to do an extesion to analyse this case. What I did is re-doing all the previous proccess and adjust the interval to just the low frecuencies. All is the same, just there are new variables:

rango_graves: highest frecuency of the new interval.

frec_cuerda6: peak of frecuency in the interval, is it exist.


Function Procesado II

deteccion2.png
detecciom.png

In the second secction of the code the frecuency is sorted into the corresponding string and the semicircular gauge adapts to the range of the stirng. As said before, the lower string has a separate secction in the code because it needs a special treatment.

The variable cuerda is where the number of the string is saved for a subsequent processing.

The frecuency is sorted in the intervals established in the step 1 by a succession of if and elseif. Also, the frecuency and the note are displayed in the corresponding label. This code is repeated for each string.

For the sixth string the code is slightly diferent, the variables used for the sorting are the ones defined for this string.

If the frecuency doesn't mach any of the intervals defined, we can't sort the strinf and the code ends there.

Function Procesado II

switch.png
switch2.png

This last part of the code is the same for every string. In this part of the code, using a switch, the tunning is checked and the dispalyed in the corresponding lable. Also, in the axis is represented with a vertical line (in red) where the tuned frecuency is supposed to be, and with a blue line where is the current frecuency we have.

For the sixth string is the exact same code, but changing frec_cuerda for frec_cuerda6.

And here the code has ended.


Code, User Guide and Video

Here are all the files, and a video of how it's used.