### Arduino FHT Library

#### FHT Functions

There are multiple functions you can call to perform the FHT. The reason they are broken up into these sections, is so you can tailor the FHT to your needs. If you don't neet particular parts, you can eliminate them and save time. There are 2 pre-processor functions *fht_reorder()* and *fht_window()*, the main function *fht_run()*, and 4 output options: linear 16b, linear 8b, logarithmic 8b, and octave 8b.

**fht_run()** - This is the main FHT 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 *fht_input[]*, which contains 1 16b value per FHT data point. If you are filling the array yourself, place real values in sequential bins. For example:

- fft_input[0] = real sample 1
- fft_input[1] = real sample 2

the final output is kept in *fht_input[]*, with the 0 -> N/2 bins having the sum of the real and imaginary components, and the N/2 -> N having the difference between the real and imaginary components. these two parts need to be squared and summed to get the overall magnitude at a particular frequency. as a result, you will have to run one of the magnitude functions to get useful data from these bins.

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

**fht_window()** - This function multiplies the input data by a window function to help increase the frequency resolution of the FHT data. It takes no variables, and returns no variables. This processes the data in *fht_input[]*, so that data must first be placed in that array before it is called. It must also be called before *fht_reorder()* or *fht_run()*.

**fht_mag_lin8()** - This gives the magnitude of each bin in the FHT. 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 *fht_input[]*, and returns the data in an array called *fht_lin_out8[]*. You can then use the data in *fht_lin_out8[]*. The magnitude is only calculated for the first N/2 bins, as the second half of an FHT is redundant. Therefore, *fht_lin_out8[]* has N/2 8b values, with each index representing the bin order. For example:

fht_lin_out8[0] = first bin magnitude (0hz -> Fs/N)

fht_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.

**fht_mag_lin()** - This gives the magnitude of each bin in the FHT. 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 *fht_input[]* and returned in *fht_lin_out[]*. The values are in sequential order, and there are only N/2 values total, as the FHT returns redundant data.

**fht_mag_log()** - This gives the magnitude of each bin in the FHT. 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*(log_{2}((img^{2} + real^{2})^{1/2}))}. It is only an 8b value, and the values are taken from *fht_input[]*, and returned in *fht_log_out[]*. The output values are in sequential order of FHT frequency bins, and there are only N/2 total bins, as the second half of the FFT result is redundant.

**fht_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 *fht_output[]* and returned in *fht_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:

- FHT_N = 256 : bins = [0, 1, 2:4, 5:8, 9:16, 17:32, 3:64, 65:128]
- FHT_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.