[Best]Chromatic Tuner Project Final Report

8
Chromatic Tuner Project Final Report By Nicholas Ambrosio Introduction For my project, I propose to design a chromatic tuner. It will essentially be an audio spectrum analyzer designed through an Arduino Fast Fourier Transform program. This tuner will detect frequencies within the concert B-flat chromatic scale using the Arduino Mega 2560 as the main component. It will have a screen that can give live FFT feedback on the tuner as well as LED displays. It will have more special features that will be determined later. My tuner will be able to analyze multiple frequencies of the original incoming sound waves. However, these tuners have a wide range of applications in both engineering and research fields. One interesting experiment in a physics laboratory would consist of testing how the environment affects the tone or overall sound quality of an instrument or voice. If the environment is too hot, the instrument will slightly expand and make the outgoing sound more flat. If the environment is too cold, the instrument will slightly shrink, making the notes sound sharper. Additionally, it can test the overall acoustics of a room, speaker, telephone, or other sound devices. Another example of testing is that my tuner will be able to test if the instrument itself is initially constructed correctly. There are several more physics applications my tuner can be implemented for. If one is studying the dissonance between notes or other frequencies, my tuner will be able to detect all incoming frequencies within a reasonable time interval and display them on the FFT display. Ultimately, this project will be mainly digital and programming heavy. This allows for potentially faster and more precise readings of the incoming audio waves. It also gives extreme flexibility on altering the device for other purposes and applications. Design Figure 1. My illustration above breaks down the project into three distinct parts: (1) microphone amplifier and DC offset, (2) Arduino Code, (3) LED Display. The main part of this project will be the coding in the Arduino.

Transcript of [Best]Chromatic Tuner Project Final Report

Page 1: [Best]Chromatic Tuner Project Final Report

Chromatic Tuner Project Final Report By Nicholas Ambrosio

Introduction

For my project, I propose to design a chromatic tuner. It will essentially be an audio spectrum analyzer designed through an Arduino Fast Fourier Transform program. This tuner will detect frequencies within the concert B-flat chromatic scale using the Arduino Mega 2560 as the main component. It will have a screen that can give live FFT feedback on the tuner as well as LED displays. It will have more special features that will be determined later. My tuner will be able to analyze multiple frequencies of the original incoming sound waves. However, these tuners have a wide range of applications in both engineering and research fields.

One interesting experiment in a physics laboratory would consist of testing how the environment affects the tone or overall sound quality of an instrument or voice. If the environment is too hot, the instrument will slightly expand and make the outgoing sound more flat. If the environment is too cold, the instrument will slightly shrink, making the notes sound sharper. Additionally, it can test the overall acoustics of a room, speaker, telephone, or other sound devices. Another example of testing is that my tuner will be able to test if the instrument itself is initially constructed correctly.

There are several more physics applications my tuner can be implemented for. If one is studying the dissonance between notes or other frequencies, my tuner will be able to detect all incoming frequencies within a reasonable time interval and display them on the FFT display. Ultimately, this project will be mainly digital and programming heavy. This allows for potentially faster and more precise readings of the incoming audio waves. It also gives extreme flexibility on altering the device for other purposes and applications. Design

Figure 1. My illustration above breaks down the project into three distinct parts: (1) microphone amplifier and DC offset, (2) Arduino Code, (3) LED Display. The main part of this project will be the coding in the Arduino.

Page 2: [Best]Chromatic Tuner Project Final Report

My design will be similar to nikoala3’s Arduino guitar tuner1 on www.instructables.com. I will be using an electret microphone to pick up the desired sound waves while minimizing noise. The signal will then be amplified with an operational amplifier and offset with capacitors so that the signal can oscillate from 0V to 5V before going into a spectrum analyzer. This is required because the Arduino ADC only detects positive voltages between 0V and 5V.

The spectrum analyzer will be designed from an Arduino Mega2560 and the Arduino software. It will be able to run a FFT (Fast Fourier Transform) on the incoming signal and decompose it into bins with varying amplitudes in units of decibels. Through a series FFT functions specialized algorithms, the bin with the strongest amplitude will indicate the frequency of the incoming signal. By comparing the frequency to the closest expected frequency, the difference between the two will prescribe whether the note is flat or sharp. There will be 7 LEDs that will indicate how off phase the frequency is. The frequency is then calculated by the following equation:

Frequency = bin number * (Sample rate / FFT size ) (1)

The sample rate is how fast the ADC (analog to digital converter) can record the incoming samples. It dictates the frequencies that can be accurately measured. Only frequencies up to half the sample rate can be accurately measured according to Nyquist-Shannon’s sampling theorem. The FFT size dictates how many samples are needed to run the FFT size and the frequency bins that are returned after running the FFT. Once the frequency is determined, it will run through numerous if statements in the Arduino program to check which known frequency it is closest to. There will be 12 LEDs displaying a concert B-flat chromatic scale. An LED will light up if the incoming sound wave is within 12 -15 Hz of the expected frequency. In other words, the bin number and bin size will essentially be the most significant variables for calculating the phase and frequency. Circuit Schematic

                                                                                                               1  Nikoala3.  “Arduino Guitar Tuner.” http://www.instructables.com/id/Arduino-Guitar-Tuner/

Figure 2. This schematic is mainly to illustrate my microphone amplifier and DC offset design. Notice that the Op-Amp has a gain of 100x and a DC offset of 2.5V.

OPA2132PA

Page 3: [Best]Chromatic Tuner Project Final Report

Microphone Amplifier and DC Offset The electret microphone takes in sound pressure that varies the spacing between two plates. This vibration causes the current to change as a result of these sound waves. There is a FET buffer within the microphone to give a slight amplification of the current which is then turned into a detectable voltage with the 10 kΩ resistor R1. The signal is then passed through a coupling capacitor to block out any DC (low-frequencies) signals where the cut off frequency can be calculated with the equation:

𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 =1

2𝜋 ∗ 𝑅𝐶                                              (2) The signal is then amplified through the op-amp (LF411 or OPA2132PA) with a gain equal to the ratio of resistor R5(100 kΩ) and resistor R3 (1 kΩ). The resistors going into pin 3 (+ pin) of the op amp are voltage dividers. The output is then sent to the Arduino. Arduino and Spectrum Analyzer Code After spending days researching how FFT programs and libraries work, I decided that creating my own FFT program from scratch is way beyond my level of expertise. Instead, I will be using an Arduino FFT library created by OpenMusicLabs.com found on their website. Basically, my program works by taking in the analog voltage of the incoming signal and translates it back into the digital bits for the Arduino program to read (ADC). The data then sorts the real data into even bins while the imaginary bins are sorted into odd bins of an array. The FFT functions then reorder the data and windows the FFT algorithm is run and outputted with the array fft_log_out. This array is now separated into 256 bins of varying frequencies. Thus, the frequency of the incoming sound wave should theoretically be the bin that has the highest intensity. Ideally, the bin width would be close to 1 Hz so that a simple algorithm of if statements could check to see how far off the incoming frequency is from the expected one. LED Display

Figure 3. This is the basic implementation for my Arduino code. It shows the basic logic on how my tuner will calculate the frequency of a signal and tune it, using a FFT library along with my self coded functions.

Figure 4. This is the basic schematic of the LED display. There will be 20 LEDs in total instead of just the two depicted here.

Page 4: [Best]Chromatic Tuner Project Final Report

The LED display will follow the pattern in the block diagram. The top 7 lights will be the tuner. The middle green light indicates that it is tuned if it is within 1 Hz of the expected frequency of that note. The LEDs are connected to a specific pin of the Arduino. All LEDs are controlled by the code shown above.

Results Pre-Amp and Offset

For this pre-amp I used 1% tolerance resistors to have a more consistent output with the input. This is not shown, but using 1% resistors has reduced the noise slightly from the standard 5%. Additionally, I tested different op amps including a LF411, a LM358, and a TL082, which gave all satisfactory output signals for this project. However, the OPA2132PA op amp gave the output as shown in figure 5. This is due to the high slew rate of 20V/us and low distortion properties of the chip. Though this project can be built using all the standard components found in the physics 117 lab, it is critical that the output signal from the pre-amp gives little distortion of the frequency as possible. This is because my algorithm for finding frequencies in the FFT data is based off a high-resolution dataset of 1 Hz per bin. FFT Output One of my more significant goals was to reproduce an FFT graph similar to one on an oscilloscope seen in figure 6. That would become my best evaluation method for the entire project. It gives me the most information about how my FFT algorithm is working as well as the frequency. Currently, I am able to produce a low-resolution graph with a 0 Hz to 20 kHz range of frequencies as seen in figure 7. Both figure 6 and figure 7 are being tested on a concert Bb at 466.16 Hz. However, there is plenty of noise that I am planning on filtering out the noise with a simple threshold algorithm. The bin number that the max amplitude occurs in figure 7 is the 4th bin. This is translated into roughly the expected frequency using the equation 1.

Figure 5. The performance of the inverting amplifier with the OPA2132PA is shown above. The original signal is the orange waveform while the output of the pre-amp is the blue one. The incoming signal is coming from a speaker that was hooked up to a function generator. It is clear that there is a DC offset of 2.5V while the amplitude is well within the desired range of 0V to 5V.

Page 5: [Best]Chromatic Tuner Project Final Report

At the moment, the resolution is at 133.14 Hz/bin, which is 66 times larger than what I want. This is by far the largest issue that I have run into with this project. The two parameters that I can freely manipulate is the sample rate of the ADC and the FFT size. Currently, the sample rate is running at about 38.5 kHz while the FFT size is 256. This required a more in depth understanding on how the ADC operates in the Arduino. A promising source2 I found emphasized that the ADC clock itself needed to be programed to a different setting.

                                                                                                               2 Berg, Guy van den. Microsmart. “Advanced Arduino – Faster analogRead().” http://www.microsmart.co.za/technical/2014/03/01/advanced-arduino-adc/

Figure 6. This is a FFT graph on the oscilloscope when a 466.16 Hz (concert Bb) is played through a speaker from a function generator into the microphone. This is the ideal result that I would like to achieve.

Figure 7. The histogram above is output data from the serial monitor of my Arduino code. It was plotted in excel to reproduce figure 6. The highest amplitude is at bin 4, which translates to about 532.56 Hz if calculated. The x-axis illustrates the range of frequencies with the y-axis as their respective intensities.

0  20  40  60  80  100  120  140  160  180  

0   2000   4000   6000   8000   10000   12000   14000   16000   18000  

Intensity  [dB]  

Frequency  [Hz]  

Concert  Bb  (466  Hz)  FFT  Output  

Page 6: [Best]Chromatic Tuner Project Final Report

Sample Rate

My first step to solving the resolution issue was to test how I could manipulate the sample rate of the ADC. The Arduino Mega2560 language is based off of open-source C libraries. The Atmel chip within the Mega specifically uses AVR libraries to expand the Arduino language. This library contains the primary functions that control the sample rate of the ADC. The Mega runs on a core clock speed of 16 MHz. The speed of the ADC clock is controlled by prescalers factors of 2, 4, 8, 16, 32, 64, and 128. The recommended ADC clock speed is from 50 kHz to 200 kHz. A normal conversion for the ADC is 13 ADC clock cycles. The sampling rate is calculated by:

𝐴𝐷𝐶  𝐶𝑙𝑜𝑐𝑘  𝑆𝑝𝑒𝑒𝑑                            16  𝑀𝐻𝑧128 = 125  𝑘𝐻𝑧

𝑆𝑎𝑚𝑝𝑙𝑖𝑛𝑔  𝑅𝑎𝑡𝑒                            125  𝐾𝐻𝑧

13 = 9615.38  𝐻𝑧 The default setting of the prescalar is at 128, making the sampling rate about 9600 Hz. In order to test this, I created a DAC to test the time interval between each sample. Notice that in figure 9 the time interval from the start of each step is about 100 microseconds. The sampling rate is found by taking the inverse of this time interval. Thus, the sampling rate is then calculated to be 1/0.100 = 10kHz. This is exactly what we expected it to be. However, Justin Colletti on www.trustmeimascientist.com expresses that a 44.1kHz-sampling rate for audio recordings is the professional and consumer standard. By manipulating the ADCSRA setting as described in figure 8, I can set the prescaler value to 32. This ultimately sets the ADC clock to 500 KHz with a sampling rate of 38.46 kHz. When I went to test this on the DAC, I was only able to get very noisy oscillations. This could be due to the fact that the sample rate is too high for the oscilloscope. However, the 38.46kHz sample rate is still verified by the frequency range (0 Hz to 17 kHz) of the FFT output. Currently, the default 9.6 kHz is the better for my algorithm, but I lose the large frequency range.  

Figure 8. My setup function for the Arduino is coded to have the ADC run on a continuous sample mode with a sample rate of 38.5 kHz by setting the ADCSRA. The additional functions here speed up the ADC even further.

Figure 9. This is the output (orange) of a 1 kHz sine wave from a function generator (blue) that was passed through the Arduino and outputted into an 8-bit DAC. This was a test to verify the sampling rate of the ADC.

Page 7: [Best]Chromatic Tuner Project Final Report

FFT Size

The second variable that I can manipulate is the FFT size. If the size of the FFT was larger, the Arduino could acquire data for a longer period of time. This would produce a higher resolution. As illustrated in figure 10, the amount of bins is directly correlated to the FFT size. To get a 1Hz resolution with the current 38.46 kHz sampling rate, we would need a FFT size of 38,460. My current FFT algorithm is only limited to a FFT size of 256 with a sample rate of 9.6 kHz. Tuner Overall Performance

At the moment, my tuner is able to determine the note that is being played into the microphone. However, it cannot “tune” the note. At higher frequencies above 1.7 kHz, my tuner would seem like it can clearly distinguish between each chromatic note. This is a natural effect caused by the frequency difference between each note is much larger than those in the lower ranges as clearly seen in figure 11. In order to quantify the quality of my project, I compared the calculated frequency outputted by the Arduino to every expected frequencies of a low C at 16.35 Hz to a high B at 7902.13 Hz. Figure 11 only displays a range from 0 Hz to 1.7 kHz because after that, there is a perfectly linear relationship. Due to the fact that my FFT algorithm has a resolution of 133.14 Hz any note 200 Hz will indicate a 𝐶! (130.81 Hz) on the LED board. I have adjusted my code so that any octave can be represented on the LED screen.

In conclusion, my tuner is only effective for higher frequency range greater than 1.7 kHz. The frequencies below that will only give the user a false impression that they are in tune.

Figure 10. This is the current raw output data taken from the serial monitor of the FFT program from a 3 kHz signal. There are a total number of 128 bins. The total bin number is half of the FFT size. Each bin has a frequency width of about 133.14 Hz. The y-axis displays the amplitude of the input frequencies.

0  20  40  60  80  100  120  140  160  180  

1   5   9   13  

17  

21  

25  

29  

33  

37  

41  

45  

49  

53  

57  

61  

65  

69  

73  

77  

81  

85  

89  

93  

97  

101  

105  

109  

113  

117  

121  

125  

Intensity  [dB]  

Bin  Number  

FFT  Size  with  a  3  kHz  Sine  Wave  

Page 8: [Best]Chromatic Tuner Project Final Report

Future Improvements At the end of quarter I was not able to complete my chromatic tuner. If I were to stick with my current algorithms, I would need to find a better way to increase the FFT size. The sample rate can be manipulated accordingly. The easiest solution that I found would be to switch from an Arduino Mega2560 to an Arduino DUE. The DUE has a few more advantages: a 84 MHz core clock speed, 3.3V system voltage, ARM libraries (instead of AVR), 2 DACs, 512 Kbytes of flash memory for code, and a 32-bit core. It is superior to the Mega 2560 in every way. However, the DUE runs on an ARM Core that is completely different from the Mega’s AVR Core. I would have to start from scratch if I were to make this switch.

An alternative solution that would require a whole different design and algorithm is implementing a phase lock loop code. Essentially, the PLL code would output the difference of phase between the incoming frequency and expected frequency. This would provide a much quicker running time in my algorithm as both the phase difference and frequency would be outputted at the same time.

Additional add-ons will include, an auto-tuner, an audio-recorder, an automatic brass tuner, and a screen that shows a graph of a live FFT calculation. The auto-tuner will basically play the note corrected pitch the user is intending to produce. A speaker will be added on to the tuner so that the outputted signal can be heard. The audio recorder will save the incoming sound waves to either a .txt file or some similar format. The automatic brass tuner will have an arm or clamp attached to the slide of a trombone slide or trumpet valve that will move to correct the pitch. The screen for the live FFT calculation is one of the more important add-ons I hope to implement. An array of LEDs will be placed on the other side of the tuner so the decomposition of frequencies can be seen in real time. Keep in mind that the circuit for this board is fairly simple. ** Warning: This project will be heavily coding based! **

Figure 11. This is a plot to illustrate how far off our calculated frequency is against the expected one. Here the output frequency of the FFT is the x-axis while the expected frequency of the note played is depicted on the y-axis. The red line represents the ideal case with a 1 Hz resolution.

y  =  0.9725x  +  32.125  R²  =  0.9935  

0  

200  

400  

600  

800  

1000  

1200  

1400  

1600  

1800  

0   200   400   600   800   1000   1200   1400   1600  

Expected  Frequeny  [Hz]  

Output  Frequency  [Hz]  

Frequency  Response