Arduino FFT庫

出自YFRobotwiki
跳轉到: 導覽搜尋

Arduino FFT (FAST FOURIER TRANSFORM 快速傅里葉變換 )Library

Notes:

  • For a fasher frequency analysis library, check out the FHT!
  • The serial output of the examples is in binary, not ASCII. This means it will not be human readable on the serial port. Change serial.write() to serial.print() to fix this. You may need to write a for() loop to manually output each frequency bin.


About the Arduino FFT Library

The Arduino FFT library is a fast implementation of a standard FFT algorithm which operates on only real data. It can give you up to 256 frequency bins at 16b depth, at a minimum of ~7ms update rate. It is adjustable from 16 to 256 bins, and has several output methods to suit various needs. It can be set to 16b linear, 8b linear, 8b logarithmic, or 8b octave output. All of these different modes are detailed in the read_me file (inside the FFT library folder). Since it takes in 16b, fixed point numbers, it has a noise floor of around -72dB in the low frequencies, and -76dB in the high frequencies. When using the onboard ADC, the ADC's noise floor is on the same order as the FFT's noise floor, giving somewhere between a 9b and 10b SNR (-55dB).



Examples

- XX :



Arduino_FFT Functions

- fft_run() - This is the main FFT function call. It takes no variables and returns no variables. It assumes there is a block of data already in SRAM, and that it is already re-ordered. The data is stored in array called fft_input[], which contains 2 16b values per FFT data point - one for real, the other for imaginary. If you are filling the array yourself, place the real values in the even bins, and the imaginary values in the odd bins. For example:

  • fft_input[0] = real1, fft_input[1] = imaginary1
  • fft_input[2] = real2, fft_input[3] = imaginary2

Therefore, there are 2 times as many points in the array as there are FFT bins. If you are using only real data (i.e. values sampled from the ADC), then put those values into the even numbered bins, and be sure to write 0 to the odd valued bins. The final output is kept in fft_input[], with the even bins being the real magnitudes, and the odd bins being the imaginary magnitudes. The bins are in sequence of increasing frequncy. For example:

  • fft_input[0] & fft_input[1] = bin0 magnitudes (0hz -> Fs/N)
  • fft_input[2] & fft_input[3] = bin1 magnitudes (Fs/N -> 2Fs/N)

You will have to run one of the magnitude functions to get useful data from these bins.

-這是主要的FFT函數調用。它不需要變數並且不返回任何變數。它假設SRAM中已經存在一個數據塊,並且已經重新排序了。數據存儲在名為fft_input []的陣列中,每個FFT數據點包含2個16b值,一個用於實數,另一個用於虛數。如果你自己填充數組,將真正的值放在均勻的分區中,並將奇數值中的虛數值。例如:

  • fft_input[0] = real1, fft_input[1] = imaginary1
  • fft_input[2] = real2, fft_input[3] = imaginary2

因此,陣列中有兩倍的FFT分區。如果您僅使用實際數據(即從ADC採樣的值),則將這些值放入均勻的數據箱中,並確保將0寫入奇數值倉。最終輸出保存在fft_input []中,偶數分數為真實幅度,奇數分數為虛數。垃圾箱按照頻率遞增的順序。例如:

  • fft_input[0] & fft_input[1] = bin0 magnitudes (0hz -> Fs/N)
  • fft_input[2] & fft_input[3] = bin1 magnitudes (Fs/N -> 2Fs/N)

您將必須運行其中一個大小函數來從這些分區獲取有用的數據。


- fft_reorder() - This reorders the FFT inputs to get them ready for the special way in which the FFT algorithm processes data. Unless you do this yourself with another piece of code, you have to call this before running fft_run(). It takes no variables and returns no variables. This runs on the array fft_input[], so the data must be first filled into that array before this function is called.

- 這將對FFT輸入進行重新排序,使其能夠準備好FFT演算法處理數據的特殊方式。 除非你自己使用另一段代碼,否則你必須在運行fft_run()之前調用它。 它不需要變數並且不返回任何變數。 這在數組fft_input[]上運行,所以在調用此函數之前,必須首先將數據填充到該數組中。


- fft_window() - This function multiplies the input data by a window function to help increase the frequency resolution of the FFT data. It takes no variables, and returns no variables. This processes the data in fft_input[], so that data must first be placed in that array before it is called. It must also be called before fft_reorder() or fft_run().

- 該功能將輸入數據乘以窗口函數,以幫助增加FFT數據的頻率解析度。 它不需要任何變數,並且不返回變數。 這將處理fft_input []中的數據,因此在調用數據之前必須首先將數據放在該數組中。 它也必須在fft_reorder()或fft_run()之前調用。


- fft_mag_lin8() - This gives the magnitude of each bin in the FFT. It sums the squares of the imaginary and real parts, and then takes the square root, rounding the answer to 8b precision (it uses a lookup table, and scales the values to fit the full 8b range. It takes no variables, and returns no variables. It operates on fft_input[], and returns the data in an array called fft_lin_out8[]. You can then use the data in fft_lin_out8[]. The magnitude is only calculated for the first N/2 bins, as the second half of an FFT is identical to the first half for all real inputs. Therefore, fft_lin_out8[] has N/2 8b values, with each index representing the bin order. For example:

  • fft_lin_out8[0] = first bin magnitude (0hz -> Fs/N)
  • fft_lin_out8[1] = second bin magnitude (Fs/N -> 2Fs/N)

The output can be scaled to maximize the resolution using the SCALE factor. See the #define section for more detials.

- 這給出了FFT中每個倉的幅度。 它將虛部和實部的平方相加,然後取平方根,將答案舍入到8b精度(它使用查找表,並將值縮放以適應完整的8b範圍,不需要變數,不返回 變數,它在fft_input []上運行,並返回一個名為fft_lin_out8 []的數組,然後可以使用fft_lin_out8 []中的數據,僅對第一個N / 2 bin進行計算, FFT與所有實際輸入的前半部分相同,因此fft_lin_out8 []具有N / 2個8b值,每個索引表示bin順序,例如:

  • fft_lin_out8[0] = first bin magnitude (0hz -> Fs/N)
  • fft_lin_out8[1] = second bin magnitude (Fs/N -> 2Fs/N)

可以縮放輸出以使用SCALE因子最大化解析度。 有關更多的detials,請參閱下文#define部分。


- fft_mag_lin() - This gives the magnitude of each bin in the FFT. It sums the squares of the imaginary and real, and then takes the square root. It uses a lookup table to calculate the square root, so it has limited precision. You can think of it as an 8b value times a 4b exponent. It covers the full 16b range, but only has 8b of precision at any point in that range. The data is taken in from fft_input[] and returned in fft_lin_out[]. The values are in sequential order, and there are only N/2 values total, as the FFT of a real signal is symetric about the center frequency.

- 這給出了FFT中每個倉的幅度。 它將虛擬和實數的平方相加,然後取平方根。 它使用查找表來計算平方根,因此精度有限。 你可以把它當成8b值乘以4b指數。 它覆蓋了整個16b範圍,但在該範圍內的任何點只有8b的精度。 數據從fft_input []中獲取,並在fft_lin_out []中返回。 這些值按順序排列,總共只有N / 2個值,因為實際信號的FFT與中心頻率相對應。


- fft_mag_log() - This gives the magnitude of each bin in the FFT. It sums the squares of the imaginary and real, and then takes the square root, and then takes the log base 2 of that value. Therefore, the output is compressed in a logarithmic fashion, and is essentially in decibels (times a scaling factor). It takes no variables, and returns no variables. It uses a lookup table to calculate the log of the square root, and scales the output over the full 8b range {the equation is 16*(log2((img2 + real2)1/2))}. It is only an 8b value, and the values are taken from fft_input[], and returned in fft_log_out[]. The output values are in sequential order of FFT frequency bins, and there are only N/2 total bins, as the second half of the FFT result is redundant for real inputs.

- 這給出了FFT中每個倉的幅度。 它將虛數和實數的平方相加,然後取平方根,然後取該值的log base 2。 因此,輸出以對數方式被壓縮,並且基本上以分貝(乘以縮放因子)。 它不需要任何變數,並且不返回變數。 它使用查找表來計算平方根的對數,並在整個8b範圍內縮放輸出 {the equation is 16*(log2((img2 + real2)1/2))} 。 它只是一個8b值,值取自fft_input[],並返回fft_log_out[]。 輸出值按照FFT頻率倉的順序排列,只有N/2個總存儲區,因為FFT結果的後半部分對於實際輸入是冗餘的。


- fft_mag_octave() - This outputs the RMS value of the bins in an octave (doubling of frequencies) format. This is more useful in some ways, as it is closer to how humans perceive sound. It doesn't take any variables, and doesn't return any variables. The input is taken from fft_output[] and returned in fft_oct_out[]. The data is represented as an 8b value of 16*log2(sqrt(mag)). There are LOG_N bins, and they are given as follows:

  • FFT_N = 256 : bins = [0, 1, 2:4, 5:8, 9:16, 17:32, 3:64, 65:128]
  • FFT_N = 128 : bins = [0, 1, 2:4, 5:8, 9:16, 17:32, 3:64]

Where, for example, (5:8) is a summation of all bins, 5 through 8. The data for each bin is squared, imaginary and real parts, and then added with all the squared magnitudes for the range. It is then divided down by the number of bins (which can be turned off - see #defines section), and the square root is taken, followed by the log being computed.

- 這將以八度(頻率倍增)格式輸出機箱的RMS值。 這在某些方面更有用,因為它更接近於人類如何看待聲音。 它不需要任何變數,也不返回任何變數。 輸入取自fft_output []並在fft_oct_out []中返回。 數據表示為8b * log2(sqrt(mag))的8b值。 有LOG_N bin,它們給出如下:

  • FFT_N = 256:bins = [0,1,2:4,5:8,9:16,17:32,3:64,65:128]
  • FFT_N = 128:bins = [0,1,2:4,5:8,9:16,17:32,3:64]

例如,(5:8)是5到8之間的所有倉的總和。每個倉的數據是平方,虛數和實數,然後加上範圍的所有平方的數值。 然後將其除以數目(可以關閉 - 請參見#defines部分),並取平方根,然後計算日誌。



Arduino_FFT #Define options

These values allow you to modify the FFT code to fit your needs. For the most part, they just turn off stuff you aren't using. By default most functions are off, so you will have to turn them on to use them. You must also declare these #defines before the #include statements in your sketch.

這些值允許您修改FFT代碼以適應您的需要。 在很大程度上,他們只是關閉你沒有使用的東西。 默認情況下,大多數功能都關閉,所以您必須打開它們才能使用它們。 您還必須在草圖中的#include語句之前聲明這些#define。

FFT_N - Sets the FFT size. Possible options are 16, 32, 64, 128, 256. 256 is the defualt. - 設置FFT大小。 可能的選項是16,32,64,128,256。默認是256。


SCALE - Sets the scaling factor for fft_mag_lin8(). Since 8b resolution is pretty poor, you will want to scale the values to max out the full range. Setting SCALE multiplies the output by a constant before doing the square root, so you have maximum resolution. It does consume slightly more resources, but is pretty minimal. SCALE can be any number from 1 -> 255. By default it is 1. 1, 2, 4, 128, and 256 consume the least resources.

- 設置fft_mag_lin8() 的縮放因子。 由於8b解析度很差,您將需要將值縮放到全範圍內。 設置SCALE在執行平方根之前將輸出乘以常數,因此您具有最大解析度。 它消耗更多的資源,但是很少。 SCALE可以是從1 -> 255的任何數字。默認情況下,它是1,2,4,128和256消耗最少的資源。


WINDOW - Turns on or off the window function resources. If you are not using fft_window(), then you should set WINDOW 0 (off). By default its 1 (on).

- 打開或關閉窗口功能資源。 如果你不使用fft_window(),那麼你應該設置WINDOW 0(off)。 默認情況下其1(開)。


REORDER - Turns on or off the reorder function resources. If you are not using fft_reorder(), then you should set REORDER 0 (off). By default its 1 (on).

- 打開或關閉重新排序功能資源。 如果你不使用fft_reorder(),那麼你應該設置REORDER 0(off)。 默認情況下其1(on)。


LOG_OUT - Turns on or off the log function resources. If you are using fft_mag_log(), then you should set LOG_OUT 1 (on). By default its 0 (off).

- 打開或關閉日誌功能資源。 如果你使用fft_mag_log(),那麼你應該設置LOG_OUT 1(on)。 默認為0(關)。


LIN_OUT - Turns on or off the lin output function resources. If you are using fft_mag_lin(), then you should set LIN_OUT 1 (on). By default its 0 (off).

- 打開或關閉lin輸出功能資源。 如果你使用fft_mag_lin(),那麼你應該設置LIN_OUT 1(on)。 默認為0(關)。


LIN_OUT8 - Turns on or off the lin8 output function resources. If you are using fft_mag_lin8(), then you should set LIN_OUT8 1 (on). By default its 0 (off).

- 打開或關閉lin8輸出功能資源。 如果你使用fft_mag_lin8(),那麼你應該設置LIN_OUT8 1(on)。 默認為0(關)。


OCTAVE - This turns on or off the octave output function resources. If you are using fft_mag_octave(), then you should set OCTAVE 1 (on). by default it is 0 (off).

- 打開或關閉八度輸出功能資源。 如果你使用fft_mag_octave(),那麼你應該設置OCTAVE 1(on)。 默認為0(關)。


OCT_NORM - This turns on or off the octave normilisation feature. This is the part of fft_mat_octave() that divides each bin grouping by the number of bins averaged. Since a lot of sound sources are pink noise (they drop off in amplitude as the frequency increases), the scale tends to drop off rather quickly. This artificially boosts the higher frequencies when off (OCT_NORM 0). By default, the normilisation is on (OCT_NORM 1).

- 這會打開或關閉八度守規矩特徵。 這是fft_mat_octave()的一部分,它將每個bin分組除以平均值的數量。 由於很多聲源都是粉紅色的噪音(隨著頻率的增加,它們的幅度會下降),所以音階會比較快地下降。 當人為關閉(OCT_NORM 0)時,人為地提高頻率。 默認情況下,正常化(OCT_NORM 1)。




返回Arduino庫菜單

返回首頁

更多建議和問題歡迎反饋至 YFRobot論壇

歡迎加入群聊:技術交流群