phyphox Forums
How did you implement the FFT in the app? - Printable Version

+- phyphox Forums (https://phyphox.org/forums)
+-- Forum: Discussion (https://phyphox.org/forums/forumdisplay.php?fid=1)
+--- Forum: General (https://phyphox.org/forums/forumdisplay.php?fid=3)
+--- Thread: How did you implement the FFT in the app? (/showthread.php?tid=746)



How did you implement the FFT in the app? - Luca Frey - 04-01-2020

Hey Phyphox Community,

I am a student at the DHBW in Karlsruhe, Germany and want to program an app for a project.
The Problem is, I am a mechanical engineer so, while I'm above average in terms of programming, when compared to my colleges, I'm certainly no software developer. In short, I need some help  Angel .

I want to build an app that can analyze the audio signal of a tone played on the piano, recorded with the built in microphone, so that I know the main frequency (root note in the harmonic overtone series) from the recorded audio. So the input is: (at best realtime-) recording of someone playing a single piano key while hitting only one string at a time (some keys on the piano hit multiple strings, but all but one will be damped so that the audio represents only one string vibrating). And the result I need is: the frequency (as above) of the signal to the first or better even second decimal.

For that purpose I wanted to do a FFT of my audio signal, but I don't even know which library to use, because I'm not sure if the language used works with my java based Android App (I use Android Studio).

Is there someone, maybe even from the developers team of Phyphox, who can explain to me how to do that and which library/methods to use?

Thanks to all answers in advance.

Luca Frey


RE: How did you implement the FFT in the app? - Sebastian Staacks - 04-02-2020

Well, we went for the fast but not exactly easy route. Phyphox uses the fftw library (as it is GNU GPL, this is only possible since phyphox is open source, too) through Android NDK. So fftw is written in c and compiled into native code for all the different architectures. This should be one of the fastest ways to do it, but it is not necessarily simple to set up. The interface between Java and C++ (NDK) can be seen in https://github.com/Staacks/phyphox-android/blob/master/app/src/main/cpp/analysis/analysis.cpp and https://github.com/Staacks/phyphox-android/blob/master/app/src/main/java/de/rwth_aachen/phyphox/Analysis.java. (Sorry, the latter is one of the older ugly parts of phyphox.)

However, I am not entirely sure if you want to use FFT for what you plan to do. The resolution of the Fourier transform is the inverse of the duration of the recording that you transform. So, if you want to resolve two decimal places, let's say 0.05 Hz, you need to analyze 1/0.05Hz = 20s. Not sure if that is what you want and if your tone is stable for 20s - and that is certainly not realtime. Depending on what you already know about your signal and depending on other trade-offs (you don't need the entire spectrum, do you? You do not expect low frequencies on top of your tone?), there are better methods to detect the frequency, but you need to research them as I only have "implementing a better frequency detection" on my todo list without having done the research yet.


RE: How did you implement the FFT in the app? - Luca Frey - 04-05-2020

At minimum I need an accuracy of 1 cent. A cent is the number of Hertz you get by dividing the distance between one note on the piano and the next (half-tone step) by 100 equally spaced steps. So a cent is the length of one of those steps.
Here are the frequencies for each key of the piano for reference: https://en.wikipedia.org/wiki/Piano_key_frequencies
Because the frequency differences of low keys are quite small (down to approximately 2 Hz) the accuracy has to be 0.02 Hz...
Fortunately this is the extrem worst case and the distance between the keys is growing faster and faster for higher and higher keys, I need an accuracy of 0.25 Hz for a key in the middle (A4=440 Hz here) and an accuracy of 2.35 Hz for the highest key (C8=4186Hz).
Do you see any chance for the measurement in the lower third of the piano? (Recording with a cellphone microphone, I should further mention)


RE: How did you implement the FFT in the app? - Sebastian Staacks - 04-05-2020

As I said, I have not done my research on that yet. You can look at our method in "Audio Autocorreclation" (this should work for clean individual tones), but I highly recommend to have a look at publications (or maybe even text books) as I am sure that there are better specialized approaches to this problem. As you are looking for example implementations, you could also try to contact the developers of guitar tuner apps (maybe there are even some open source ones). The simple ones might be content with an FFT, but I can imagine that some of them prefer to be a bit more efficient.