fft (incomplete)

This commit is contained in:
Killorin 2025-04-17 12:32:11 -04:00
parent 264fc2c4ec
commit 22a3b5e795
2 changed files with 78 additions and 55 deletions

107
main.c
View file

@ -31,18 +31,32 @@
#include "driverlib/pin_map.h"
#include "sampling.h"
#include "buttons.h"
#include "kiss_fft.h"
#include "_kiss_fft_guts.h"
#define PWM_FREQUENCY 20000 // PWM frequency = 20 kHz
uint32_t gSystemClock = 120000000; // [Hz] system clock frequency
#define PI 3.14159265358979f
tContext sContext;
uint32_t cputime_unloaded;
#define FFT_EN false
#define LOCAL_BUF_LEN 128
uint16_t adc_buffer_sample[LOCAL_BUF_LEN]; // copy of g adc buffer
uint8_t adc_buffer_processed[LOCAL_BUF_LEN]; // copy of g adc buffer
#define FFT_BUF_LEN 1024
uint16_t adc_buffer_fft_sample[FFT_BUF_LEN]; // copy of g adc buffer
uint8_t adc_buffer_fft_processed[FFT_BUF_LEN]; // copy of g adc buffer
kiss_fft_cfg cfg;
static kiss_fft_cpx in[FFT_BUF_LEN], out[FFT_BUF_LEN];
typedef struct
{
uint8_t voltage_scale;
@ -151,32 +165,37 @@ int main(void)
GrContextInit(&sContext, &g_sCrystalfontz128x128); // Initialize the grlib graphics context
GrContextFontSet(&sContext, &g_sFontFixed6x8); // select font
#define KISS_FFT_CFG_SIZE (sizeof(struct kiss_fft_state)+sizeof(kiss_fft_cpx)*(FFT_BUF_LEN-1))
static char kiss_fft_cfg_buffer[KISS_FFT_CFG_SIZE];
size_t buffer_size = KISS_FFT_CFG_SIZE;
cfg = kiss_fft_alloc(FFT_BUF_LEN, 0, kiss_fft_cfg_buffer, &buffer_size);
/* Start BIOS */
BIOS_start();
return (0);
}
void task0_func(UArg arg1, UArg arg2)
{
IntMasterEnable();
while (true) {
// do nothing
}
}
void capture_waveform(UArg arg1, UArg arg2)
{
IntMasterEnable();
while(true) {
Semaphore_pend(capture_sem, BIOS_WAIT_FOREVER);
int32_t adc_current_index;
int32_t i;
if (true) {
adc_current_index = gADCBufferIndex;
for(i=adc_current_index-FFT_BUF_LEN; i<adc_current_index; i++) {
//in[i].r = sinf(20*PI*i/FFT_BUF_LEN);
in[i].r = (float) gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + i)];
in[i].i = 0;
}
} else {
// single read of options is atomic
int32_t trigger_mode = options.trigger_mode;
int32_t adc_current_index;
int32_t j;
int trigger;
if (trigger_mode == 2) {
trigger = gADCBufferIndex - (WIDTH / 2); // show latest if trigger disabled
@ -185,30 +204,55 @@ void capture_waveform(UArg arg1, UArg arg2)
}
adc_current_index = trigger - (WIDTH / 2);
for (j=0; j<LOCAL_BUF_LEN; j++) {
adc_buffer_sample[j] = gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + j)];
for (i=0; i<LOCAL_BUF_LEN; i++) {
adc_buffer_sample[i] = gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + i)];
}
}
Semaphore_post(process_sem);
}
}
void process_waveform(UArg arg1, UArg arg2) {
static float w[FFT_BUF_LEN];
int i;
for (i=0; i<FFT_BUF_LEN; i++) {
w[i] = 0.42f
- 0.5f * cosf(2*PI*i/(FFT_BUF_LEN-1))
+ 0.08f * cosf(4*PI*i/(FFT_BUF_LEN-1));
}
while(true) {
Semaphore_pend(process_sem, BIOS_WAIT_FOREVER);
int i;
// single read of global options is atomic
float fVoltsPerDiv = gVoltageScale[options.voltage_scale];
float fScale = (VIN_RANGE * PIXELS_PER_DIV)/((1 << ADC_BITS) * fVoltsPerDiv);
int j;
for(j=0; j<LOCAL_BUF_LEN; j++) {
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define CONSTRAIN(x) MAX(MIN(HEIGHT - 1, x), 0)
if(true) { // fft
for(i=0; i<FFT_BUF_LEN; i++) {
in[i].r *= w[i];
}
kiss_fft(cfg, in, out);
for(i=0; i<LOCAL_BUF_LEN; i++) {
#define DB(out) 10 * log10f(out.r * out.r + out.i * out.i)
//adc_buffer_processed[i] = CONSTRAIN(HEIGHT- (int)roundf(0.5 * DB(out[i])));
adc_buffer_processed[i] = CONSTRAIN(HEIGHT- (int) (DB(out[i])) + 64);
}
} else {
// single read of global options is atomic
float fVoltsPerDiv = gVoltageScale[options.voltage_scale];
float fScale = (VIN_RANGE * PIXELS_PER_DIV)/((1 << ADC_BITS) * fVoltsPerDiv);
for(i=0; i<LOCAL_BUF_LEN; i++) {
#define TRANSPOSE(x) CONSTRAIN((HEIGHT/2) - (int)roundf(fScale * ((int)x - ADC_OFFSET)))
adc_buffer_processed[j] = TRANSPOSE(adc_buffer_sample[j]);
adc_buffer_processed[i] = TRANSPOSE(adc_buffer_sample[i]);
}
}
Semaphore_post(display_sem);
@ -305,25 +349,8 @@ void display_waveform(UArg arg1, UArg arg2)
// display graph
GrContextForegroundSet(&sContext, ClrYellow);
int j;
for(j=0; j<LOCAL_BUF_LEN; j++) {
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
uint32_t upper,lower, current, last;
if (j==0) {
upper = adc_buffer_processed[j];
lower = upper;
last = upper;
} else {
current = adc_buffer_processed[j];
upper = MAX(current, last);
lower = MIN(current, last);
last = current;
}
GrLineDrawV(&sContext, j, lower, upper);
for(j=1; j<LOCAL_BUF_LEN; j++) {
GrLineDraw(&sContext, j-1, adc_buffer_processed[j-1], j, adc_buffer_processed[j]);
}
GrFlush(&sContext); // flush the frame buffer to the LCD

View file

@ -536,10 +536,6 @@ var driversConfig = xdc.useModule('ti.drivers.Config');
driversConfig.libType = driversConfig.LibType_NonInstrumented;
Clock.timerId = 0;
TimestampProvider.useClockTimer = true;
var task0Params = new Task.Params();
task0Params.instance.name = "task0";
task0Params.priority = 1;
Program.global.task0 = Task.create("&task0_func", task0Params);
var m3Hwi0Params = new m3Hwi.Params();
m3Hwi0Params.instance.name = "m3Hwi0";
m3Hwi0Params.priority = 0;