Make the value type configurable

Currently, value_type is typedef'd as double.
This commit is contained in:
Thomas Kolb 2012-09-03 17:16:14 +02:00
parent 29b576377b
commit 9f196ba5e2
4 changed files with 31 additions and 29 deletions

View File

@ -51,4 +51,6 @@
typedef int16_t sample; typedef int16_t sample;
typedef int64_t sample_sum; typedef int64_t sample_sum;
typedef double value_type;
#endif // CONFIG_H #endif // CONFIG_H

22
fft.c
View File

@ -16,7 +16,7 @@
#include "fft.h" #include "fft.h"
double hanning_buffer[BLOCK_LEN]; value_type hanning_buffer[BLOCK_LEN];
int lookup_table[BLOCK_LEN]; int lookup_table[BLOCK_LEN];
@ -40,7 +40,7 @@ void init_fft(void) {
void complex_to_absolute(double *re, double *im, double *result) { void complex_to_absolute(value_type *re, value_type *im, value_type *result) {
int i; int i;
for(i = 0; i < DATALEN; i++) for(i = 0; i < DATALEN; i++)
@ -60,16 +60,16 @@ void apply_hanning(sample *dftinput) {
void fft_transform(sample *samples, double *resultRe, double *resultIm) { void fft_transform(sample *samples, value_type *resultRe, value_type *resultIm) {
int i; int i;
int layer, part, element; int layer, part, element;
int num_parts, num_elements; int num_parts, num_elements;
int left, right; int left, right;
double x_left_re, x_left_im, x_right_re, x_right_im; value_type x_left_re, x_left_im, x_right_re, x_right_im;
double param; value_type param;
double sinval, cosval; value_type sinval, cosval;
// re-arrange the input array according to the lookup table // re-arrange the input array according to the lookup table
// and store it into the real output array (as the input is obviously real). // and store it into the real output array (as the input is obviously real).
@ -122,9 +122,9 @@ void fft_transform(sample *samples, double *resultRe, double *resultIm) {
} }
} }
uint32_t find_loudest_frequency(double *absFFT) { uint32_t find_loudest_frequency(value_type *absFFT) {
int maxPos = 0; int maxPos = 0;
double maxVal = 0; value_type maxVal = 0;
int i; int i;
for(i = 0; i < BLOCK_LEN; i++) { for(i = 0; i < BLOCK_LEN; i++) {
@ -134,15 +134,15 @@ uint32_t find_loudest_frequency(double *absFFT) {
} }
} }
return (double)maxPos * SAMPLE_RATE / BLOCK_LEN; return (value_type)maxPos * SAMPLE_RATE / BLOCK_LEN;
} }
double get_energy_in_band(double *fft, uint32_t minFreq, uint32_t maxFreq) { value_type get_energy_in_band(value_type *fft, uint32_t minFreq, uint32_t maxFreq) {
int firstBlock = minFreq * BLOCK_LEN / SAMPLE_RATE; int firstBlock = minFreq * BLOCK_LEN / SAMPLE_RATE;
int lastBlock = maxFreq * BLOCK_LEN / SAMPLE_RATE; int lastBlock = maxFreq * BLOCK_LEN / SAMPLE_RATE;
int i; int i;
double energy = 0; value_type energy = 0;
for(i = firstBlock; i < lastBlock; i++) { for(i = firstBlock; i < lastBlock; i++) {
energy += fft[i]; energy += fft[i];
} }

8
fft.h
View File

@ -13,10 +13,10 @@
#include "config.h" #include "config.h"
void init_fft(void); void init_fft(void);
void complex_to_absolute(double *re, double *im, double *result); void complex_to_absolute(value_type *re, value_type *im, value_type *result);
void apply_hanning(sample *dftinput); void apply_hanning(sample *dftinput);
void fft_transform(sample *samples, double *resultRe, double *resultIm); void fft_transform(sample *samples, value_type *resultRe, value_type *resultIm);
uint32_t find_loudest_frequency(double *absFFT); uint32_t find_loudest_frequency(value_type *absFFT);
double get_energy_in_band(double *fft, uint32_t minFreq, uint32_t maxFreq); value_type get_energy_in_band(value_type *fft, uint32_t minFreq, uint32_t maxFreq);
#endif // FFT_H #endif // FFT_H

28
main.c
View File

@ -22,7 +22,7 @@
#include "config.h" #include "config.h"
// Frames per second // Frames per second
#define FPS ((double)BUFFER_PARTS * SAMPLE_RATE / BLOCK_LEN) #define FPS ((value_type)BUFFER_PARTS * SAMPLE_RATE / BLOCK_LEN)
// Number of new samples put into the buffer each frame // Number of new samples put into the buffer each frame
#define READ_SAMPLES (BLOCK_LEN / BUFFER_PARTS) #define READ_SAMPLES (BLOCK_LEN / BUFFER_PARTS)
@ -30,10 +30,10 @@
#define COLORBUF_SIZE (2*(NUM_MODULES+1)) #define COLORBUF_SIZE (2*(NUM_MODULES+1))
#define CENTER_POS (2*CENTER_MODULE) #define CENTER_POS (2*CENTER_MODULE)
double fft[BLOCK_LEN]; value_type fft[BLOCK_LEN];
double rms; value_type rms;
double redEnergy, greenEnergy, blueEnergy; value_type redEnergy, greenEnergy, blueEnergy;
double lastUpdateTime = 0; value_type lastUpdateTime = 0;
sem_t fftSemaphore; sem_t fftSemaphore;
@ -42,9 +42,9 @@ int running = 1;
void* fft_thread(void *param) { void* fft_thread(void *param) {
sample buffer[BLOCK_LEN]; sample buffer[BLOCK_LEN];
sample block[BLOCK_LEN]; sample block[BLOCK_LEN];
double fftOutReal[BLOCK_LEN], fftOutImag[BLOCK_LEN]; value_type fftOutReal[BLOCK_LEN], fftOutImag[BLOCK_LEN];
double tmpFFT[BLOCK_LEN]; value_type tmpFFT[BLOCK_LEN];
double tmpRMS; value_type tmpRMS;
double nextFrame = get_hires_time() + 0.05; double nextFrame = get_hires_time() + 0.05;
double curTime; double curTime;
@ -113,11 +113,11 @@ void* fft_thread(void *param) {
return NULL; return NULL;
} }
double gamma_correct(double d) { value_type gamma_correct(value_type d) {
return pow(d, GAMMA); return pow(d, GAMMA);
} }
void text_bar(double fill) { void text_bar(value_type fill) {
int fillCnt = 10 * fill; int fillCnt = 10 * fill;
int i; int i;
@ -130,13 +130,13 @@ void text_bar(double fill) {
} }
} }
double weighted_avg(uint8_t colorBuf[COLORBUF_SIZE][3], int channel, int centerPos) { value_type weighted_avg(uint8_t colorBuf[COLORBUF_SIZE][3], int channel, int centerPos) {
return 0.20 * colorBuf[centerPos - 1][channel] + return 0.20 * colorBuf[centerPos - 1][channel] +
0.60 * colorBuf[centerPos][channel] + 0.60 * colorBuf[centerPos][channel] +
0.20 * colorBuf[centerPos + 1][channel]; 0.20 * colorBuf[centerPos + 1][channel];
} }
void show_status(double curRed, double maxRed, double curGreen, double maxGreen, double curBlue, double maxBlue) { void show_status(value_type curRed, value_type maxRed, value_type curGreen, value_type maxGreen, value_type curBlue, value_type maxBlue) {
printf("\r"); printf("\r");
printf("[\033[31m"); printf("[\033[31m");
@ -167,8 +167,8 @@ int main(int argc, char **argv) {
uint8_t colorBuf[COLORBUF_SIZE][3]; uint8_t colorBuf[COLORBUF_SIZE][3];
double curRedEnergy, curGreenEnergy, curBlueEnergy; value_type curRedEnergy, curGreenEnergy, curBlueEnergy;
double maxRedEnergy = 1, maxGreenEnergy = 1, maxBlueEnergy = 1; value_type maxRedEnergy = 1, maxGreenEnergy = 1, maxBlueEnergy = 1;
memset(colorBuf, 0, sizeof(colorBuf)); memset(colorBuf, 0, sizeof(colorBuf));