Graphing the pitch (frequency) of a sound

前端 未结 3 1415
面向向阳花
面向向阳花 2020-12-07 10:10

I want to plot the pitch of a sound into a graph.

Currently I can plot the amplitude. The graph below is created by the data returned by getUnscaledAmplitude()

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 10:44

    Fast Fourier Transform doesn't need to know more then the input bytes you have. Don't be scared off by the Wikipedia article. An FFT algorithm will take your input signal (with the common FFT algorithms the number of samples is required to be a power of 2, e.g. 256, 512, 1024) and return a vector of complex numbers with the same size. Because your input is real, not complex, (imaginary portion set to zero) the returned vector will be symmetric. Only half of it will contain data. Since you do not care about the phase you can simply take the magnitude of the complex numbers, which is sqrt(a^2+b^2). Just taking the absoulte value of a complex number may also work, in some languages this is equivalent to the previous expression.

    There are Java implementations of FFT available, e.g.: http://www.cs.princeton.edu/introcs/97data/FFT.java.html

    Pseudo code will look something like:

    Complex in[1024];
    Complex out[1024];
    Copy your signal into in
    FFT(in, out)
    for every member of out compute sqrt(a^2+b^2)
    To find frequency with highest power scan for the maximum value in the first 512 points in out
    

    The output will contain entires for frequencies between zero and half your sampling frequency.

    Since FFT assumes a repeating signal you may want to apply a window to your input signal. But don't worry about this at first.

    You can find more information on the web, e.g.: FFT for beginners

    Also as Oli notes when multiple frequencies are present the perceived pitch is a more complex phenomenon.

提交回复
热议问题