feat: ADC buffer count

This commit is contained in:
liuyihui 2023-04-28 18:22:38 +08:00
parent 824e73237b
commit 085242a154
7 changed files with 31 additions and 20 deletions

View File

@ -5,6 +5,7 @@
#define ADC_M 2048
extern int16_t adc_buf[ADC_M];
extern uint16_t pADC;
extern uint16_t cADC;
extern uint8_t fADC;
void adc0_Init();

View File

@ -2,7 +2,7 @@
#include "fft.h"
extern const q15_t hanning_117[];
extern const q15_t hanning[];
extern const uint16_t bitrevTable[];
extern const q15_t rotCoef[];

View File

@ -6,12 +6,9 @@
#define FS 10000
#define RE FS / FFT_N
#define FREQ_BIT1 1100
#define FREQ_SYNC 1200
#define FREQ_BIT0 1300
#define FREQ_START 1900
#define FREQ_BLACK 1500
#define FREQ_WHITE 4000
#define FREQ_SYNC 4500
#define FREQ_RANGE FREQ_WHITE - FREQ_BLACK
#define COLOR_SCALE 32

View File

@ -3,6 +3,7 @@
int16_t adc_buf[ADC_M];
uint16_t pADC = 0;
uint16_t cADC = 0;
uint8_t fADC = 0;
void adc0_Init() {
@ -30,8 +31,16 @@ int16_t adc0_TR_DATA() {
static uint16_t lastPos = 0;
void adc0_read_buf(int16_t *data, uint16_t len) {
for (uint16_t i = 0; i < len; i++) {
data[i] = adc_buf[lastPos];
if (cADC > ADC_M) {
cADC = ADC_M;
lastPos = pADC;
}
while (len) {
if (cADC) {
*data++ = adc_buf[lastPos];
lastPos = (lastPos + 1) % ADC_M;
cADC--;
len--;
}
}
}

View File

@ -12,6 +12,7 @@ void SysTick_Handler(void) {
if (fADC) {
adc_buf[pADC] = adc0_TR_DATA() - 1550;
pADC = (pADC + 1) % ADC_M;
cADC++;
}
}

View File

@ -8,9 +8,9 @@
static q15_t res[FFT_N * 2];
static void hanning(q15_t *data, uint16_t len) {
static void applyWindow(q15_t *data, uint16_t len) {
for (uint16_t i = len; i < FFT_N; i++) data[i] = 0;
for (uint16_t i = 0; i < len; i++) data[i] = (q15_t)(((q31_t)data[i] * hanning_117[i]) >> 15);
for (uint16_t i = 0; i < len; i++) data[i] = (q15_t)(((q31_t)data[i] * hanning[i]) >> 15);
}
static inline void bitreversal(q31_t *data) {
@ -88,7 +88,7 @@ static inline void butterfly(q15_t *data) {
}
void FFT(q15_t *data, uint16_t len) {
hanning(data, len);
applyWindow(data, len);
for (uint16_t i = 0; i < FFT_N; i++) {
res[Re(i)] = data[i]; // real
res[Im(i)] = 0; // imaginary

View File

@ -1,14 +1,17 @@
#include "fft/table.h"
const q15_t hanning_117[] = {
0, 24, 96, 216, 383, 597, 858, 1164, 1514, 1909, 2345, 2823, 3341, 3897, 4489,
5117, 5777, 6469, 7190, 7937, 8710, 9505, 10320, 11153, 12001, 12862, 13733, 14613, 15497, 16384,
17271, 18155, 19035, 19906, 20767, 21615, 22448, 23263, 24058, 24831, 25578, 26299, 26991, 27651, 28279,
28871, 29427, 29945, 30423, 30859, 31254, 31604, 31910, 32171, 32385, 32552, 32672, 32744, 32767, 32744,
32672, 32552, 32385, 32171, 31910, 31604, 31254, 30859, 30423, 29945, 29427, 28871, 28279, 27651, 26991,
26299, 25578, 24831, 24058, 23263, 22448, 21615, 20767, 19906, 19035, 18155, 17271, 16384, 15497, 14613,
13733, 12862, 12001, 11153, 10320, 9505, 8710, 7937, 7190, 6469, 5777, 5117, 4489, 3897, 3341,
2823, 2345, 1909, 1514, 1164, 858, 597, 383, 216, 96, 24, 0,
const q15_t hanning[] = {
0, 11, 43, 96, 171, 266, 383, 521, 679, 858, 1057, 1276, 1514, 1772, 2049, 2345,
2659, 2991, 3341, 3707, 4090, 4489, 4904, 5333, 5777, 6235, 6706, 7190, 7685, 8192, 8710, 9237,
9774, 10320, 10873, 11434, 12001, 12574, 13151, 13733, 14319, 14907, 15497, 16088, 16680, 17271, 17861, 18449,
19035, 19617, 20194, 20767, 21334, 21895, 22448, 22994, 23531, 24058, 24576, 25083, 25578, 26062, 26533, 26991,
27435, 27864, 28279, 28678, 29061, 29427, 29777, 30109, 30423, 30719, 30996, 31254, 31492, 31711, 31910, 32089,
32247, 32385, 32502, 32597, 32672, 32725, 32757, 32767, 32757, 32725, 32672, 32597, 32502, 32385, 32247, 32089,
31910, 31711, 31492, 31254, 30996, 30719, 30423, 30109, 29777, 29427, 29061, 28678, 28279, 27864, 27435, 26991,
26533, 26062, 25578, 25083, 24576, 24058, 23531, 22994, 22448, 21895, 21334, 20767, 20194, 19617, 19035, 18449,
17861, 17271, 16680, 16088, 15497, 14907, 14319, 13733, 13151, 12574, 12001, 11434, 10873, 10320, 9774, 9237,
8710, 8192, 7685, 7190, 6706, 6235, 5777, 5333, 4904, 4489, 4090, 3707, 3341, 2991, 2659, 2345,
2049, 1772, 1514, 1276, 1057, 858, 679, 521, 383, 266, 171, 96, 43, 11, 0,
};
/*