From 085242a154b56ff8dd4fb4ea0b414a2d281fb5fb Mon Sep 17 00:00:00 2001 From: liuyihui Date: Fri, 28 Apr 2023 18:22:38 +0800 Subject: [PATCH] feat: ADC buffer count --- include/adc.h | 1 + include/fft/table.h | 2 +- include/sstv.h | 5 +---- src/adc.c | 15 ++++++++++++--- src/derivative.c | 1 + src/fft/fft.c | 6 +++--- src/fft/table.c | 21 ++++++++++++--------- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/include/adc.h b/include/adc.h index ef699d1..0f67a22 100644 --- a/include/adc.h +++ b/include/adc.h @@ -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(); diff --git a/include/fft/table.h b/include/fft/table.h index 674f3da..03ce99a 100644 --- a/include/fft/table.h +++ b/include/fft/table.h @@ -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[]; diff --git a/include/sstv.h b/include/sstv.h index ae8c1c0..9e7082c 100644 --- a/include/sstv.h +++ b/include/sstv.h @@ -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 diff --git a/src/adc.c b/src/adc.c index 54f8528..1637eec 100644 --- a/src/adc.c +++ b/src/adc.c @@ -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]; - lastPos = (lastPos + 1) % ADC_M; + if (cADC > ADC_M) { + cADC = ADC_M; + lastPos = pADC; + } + while (len) { + if (cADC) { + *data++ = adc_buf[lastPos]; + lastPos = (lastPos + 1) % ADC_M; + cADC--; + len--; + } } } diff --git a/src/derivative.c b/src/derivative.c index fe4d074..7be70f7 100644 --- a/src/derivative.c +++ b/src/derivative.c @@ -12,6 +12,7 @@ void SysTick_Handler(void) { if (fADC) { adc_buf[pADC] = adc0_TR_DATA() - 1550; pADC = (pADC + 1) % ADC_M; + cADC++; } } diff --git a/src/fft/fft.c b/src/fft/fft.c index 33f0adc..fbe9e54 100644 --- a/src/fft/fft.c +++ b/src/fft/fft.c @@ -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 diff --git a/src/fft/table.c b/src/fft/table.c index 785ea3c..e719d4a 100644 --- a/src/fft/table.c +++ b/src/fft/table.c @@ -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, }; /*