fft (incomplete)
This commit is contained in:
parent
264fc2c4ec
commit
22a3b5e795
2 changed files with 78 additions and 55 deletions
129
main.c
129
main.c
|
@ -31,18 +31,32 @@
|
||||||
#include "driverlib/pin_map.h"
|
#include "driverlib/pin_map.h"
|
||||||
#include "sampling.h"
|
#include "sampling.h"
|
||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
|
#include "kiss_fft.h"
|
||||||
|
#include "_kiss_fft_guts.h"
|
||||||
|
|
||||||
#define PWM_FREQUENCY 20000 // PWM frequency = 20 kHz
|
#define PWM_FREQUENCY 20000 // PWM frequency = 20 kHz
|
||||||
|
|
||||||
uint32_t gSystemClock = 120000000; // [Hz] system clock frequency
|
uint32_t gSystemClock = 120000000; // [Hz] system clock frequency
|
||||||
|
|
||||||
|
#define PI 3.14159265358979f
|
||||||
|
|
||||||
tContext sContext;
|
tContext sContext;
|
||||||
|
|
||||||
uint32_t cputime_unloaded;
|
uint32_t cputime_unloaded;
|
||||||
|
|
||||||
|
#define FFT_EN false
|
||||||
|
|
||||||
#define LOCAL_BUF_LEN 128
|
#define LOCAL_BUF_LEN 128
|
||||||
uint16_t adc_buffer_sample[LOCAL_BUF_LEN]; // copy of g adc buffer
|
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
|
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
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t voltage_scale;
|
uint8_t voltage_scale;
|
||||||
|
@ -151,64 +165,94 @@ int main(void)
|
||||||
GrContextInit(&sContext, &g_sCrystalfontz128x128); // Initialize the grlib graphics context
|
GrContextInit(&sContext, &g_sCrystalfontz128x128); // Initialize the grlib graphics context
|
||||||
GrContextFontSet(&sContext, &g_sFontFixed6x8); // select font
|
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 */
|
/* Start BIOS */
|
||||||
BIOS_start();
|
BIOS_start();
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void task0_func(UArg arg1, UArg arg2)
|
|
||||||
{
|
|
||||||
IntMasterEnable();
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void capture_waveform(UArg arg1, UArg arg2)
|
void capture_waveform(UArg arg1, UArg arg2)
|
||||||
{
|
{
|
||||||
IntMasterEnable();
|
IntMasterEnable();
|
||||||
while(true) {
|
while(true) {
|
||||||
Semaphore_pend(capture_sem, BIOS_WAIT_FOREVER);
|
Semaphore_pend(capture_sem, BIOS_WAIT_FOREVER);
|
||||||
|
|
||||||
// single read of options is atomic
|
|
||||||
int32_t trigger_mode = options.trigger_mode;
|
|
||||||
|
|
||||||
int32_t adc_current_index;
|
int32_t adc_current_index;
|
||||||
int32_t j;
|
int32_t i;
|
||||||
int trigger;
|
|
||||||
if (trigger_mode == 2) {
|
if (true) {
|
||||||
trigger = gADCBufferIndex - (WIDTH / 2); // show latest if trigger disabled
|
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 {
|
} else {
|
||||||
trigger = Trigger(trigger_mode);
|
// single read of options is atomic
|
||||||
}
|
int32_t trigger_mode = options.trigger_mode;
|
||||||
|
|
||||||
adc_current_index = trigger - (WIDTH / 2);
|
int trigger;
|
||||||
for (j=0; j<LOCAL_BUF_LEN; j++) {
|
if (trigger_mode == 2) {
|
||||||
adc_buffer_sample[j] = gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + j)];
|
trigger = gADCBufferIndex - (WIDTH / 2); // show latest if trigger disabled
|
||||||
}
|
} else {
|
||||||
|
trigger = Trigger(trigger_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
adc_current_index = trigger - (WIDTH / 2);
|
||||||
|
for (i=0; i<LOCAL_BUF_LEN; i++) {
|
||||||
|
adc_buffer_sample[i] = gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + i)];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Semaphore_post(process_sem);
|
Semaphore_post(process_sem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_waveform(UArg arg1, UArg arg2) {
|
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) {
|
while(true) {
|
||||||
Semaphore_pend(process_sem, BIOS_WAIT_FOREVER);
|
Semaphore_pend(process_sem, BIOS_WAIT_FOREVER);
|
||||||
|
int i;
|
||||||
|
|
||||||
// single read of global options is atomic
|
#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)
|
||||||
|
|
||||||
float fVoltsPerDiv = gVoltageScale[options.voltage_scale];
|
if(true) { // fft
|
||||||
float fScale = (VIN_RANGE * PIXELS_PER_DIV)/((1 << ADC_BITS) * fVoltsPerDiv);
|
for(i=0; i<FFT_BUF_LEN; i++) {
|
||||||
|
in[i].r *= w[i];
|
||||||
|
}
|
||||||
|
|
||||||
int j;
|
kiss_fft(cfg, in, out);
|
||||||
for(j=0; j<LOCAL_BUF_LEN; j++) {
|
|
||||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
for(i=0; i<LOCAL_BUF_LEN; i++) {
|
||||||
#define MAX(a,b) (((a)>(b))?(a):(b))
|
#define DB(out) 10 * log10f(out.r * out.r + out.i * out.i)
|
||||||
#define CONSTRAIN(x) MAX(MIN(HEIGHT - 1, x), 0)
|
|
||||||
#define TRANSPOSE(x) CONSTRAIN((HEIGHT/2) - (int)roundf(fScale * ((int)x - ADC_OFFSET)))
|
//adc_buffer_processed[i] = CONSTRAIN(HEIGHT- (int)roundf(0.5 * DB(out[i])));
|
||||||
adc_buffer_processed[j] = TRANSPOSE(adc_buffer_sample[j]);
|
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[i] = TRANSPOSE(adc_buffer_sample[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Semaphore_post(display_sem);
|
Semaphore_post(display_sem);
|
||||||
|
@ -305,25 +349,8 @@ void display_waveform(UArg arg1, UArg arg2)
|
||||||
// display graph
|
// display graph
|
||||||
GrContextForegroundSet(&sContext, ClrYellow);
|
GrContextForegroundSet(&sContext, ClrYellow);
|
||||||
int j;
|
int j;
|
||||||
for(j=0; j<LOCAL_BUF_LEN; j++) {
|
for(j=1; j<LOCAL_BUF_LEN; j++) {
|
||||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
GrLineDraw(&sContext, j-1, adc_buffer_processed[j-1], j, adc_buffer_processed[j]);
|
||||||
#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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GrFlush(&sContext); // flush the frame buffer to the LCD
|
GrFlush(&sContext); // flush the frame buffer to the LCD
|
||||||
|
|
4
rtos.cfg
4
rtos.cfg
|
@ -536,10 +536,6 @@ var driversConfig = xdc.useModule('ti.drivers.Config');
|
||||||
driversConfig.libType = driversConfig.LibType_NonInstrumented;
|
driversConfig.libType = driversConfig.LibType_NonInstrumented;
|
||||||
Clock.timerId = 0;
|
Clock.timerId = 0;
|
||||||
TimestampProvider.useClockTimer = true;
|
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();
|
var m3Hwi0Params = new m3Hwi.Params();
|
||||||
m3Hwi0Params.instance.name = "m3Hwi0";
|
m3Hwi0Params.instance.name = "m3Hwi0";
|
||||||
m3Hwi0Params.priority = 0;
|
m3Hwi0Params.priority = 0;
|
||||||
|
|
Loading…
Reference in a new issue