-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfft256_f32.cpp
63 lines (57 loc) · 1.78 KB
/
fft256_f32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* EasyListener for Teensy Audio Library
*
* (c) 2020 Clemens Wegener
* Bauhaus-Universität Weimar
* Department Interface Design
*
* This library uses machine learning to detect previously
* presented audio signals on the Teensy board.
*
* BSD license
*/
#include "fft256_f32.h"
static void copy_to_buffer(int16_t *destination, const int16_t *source)
{
for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
*destination++ = *source++;
}
}
static void apply_window_to_fft_buffer(void *buffer, const void *window)
{
int16_t *buf = (int16_t *)buffer;
const int16_t *win = (int16_t *)window;;
for (int i=0; i < 256; i++) {
int32_t val = *buf * *win;
*buf = val >> 15;
buf++;
win++;
}
}
void FFT256F32::process(int16_t *data, uint16_t frame_num)
{
if (!data) {
return;
}
if (!prevdata) {
copy_to_buffer(prevdata,data);
return;
}
copy_to_buffer(buffer, prevdata);
copy_to_buffer(buffer+128, data);
if (window) apply_window_to_fft_buffer(buffer, window);
arm_q15_to_float(buffer, _float_buffer, FFT_SIZE);
/* Process the data through the CFFT/CIFFT module */
arm_rfft_fast_f32(&_fft_inst, _float_buffer, _fftOutput, 0);
/* Process the data through the Complex Magnitude Module for
calculating the magnitude at each bin */
arm_cmplx_mag_f32(_fftOutput+2, _fftMag+1, FFT_SIZE/2 - 1);
// Handle special cases
_fftMag[0] = fabs(_fftOutput[0]); // the DC bin is always real (no imaginary component), but can be negative
_fftMag[FFT_SIZE/2] = fabs(_fftOutput[1]); // same for the nyquist component, read documentation of arm_rfft_fast_f32() for details
/* scale output to get correct amplitude factors */
arm_scale_f32(_fftMag, _scaleFactor , _fftMag, FFT_SIZE/2);
copy_to_buffer(prevdata,data);
_frame_num = frame_num;
outputflag = true;
}