Hi Community,
I have a set of FFT codes that is suppose to create an array of number after processing the incoming signal.
When I build my codes in code composer studio it was successfully built.
However, when I run it on debug mode it face problems.
The purpose of my project is to simply program a set of FFT algorithm in c, and allow my LM3s8962 micro-controller to process the incoming signal(Voice or sound).
I have another question, can I use the algorithm to run it in my keil uvision, since the keil uvision has the spectrum/logic analyser which I can see the frequency spectrum.
I will attach my algorithms here, if any kind hearted soul spot any mistakes in my algorithm please enlighten me.
/* * main.c */#include <stdio.h>#include <math.h>#include <stdbool.h>#include <stdint.h>#include "inc/hw_memmap.h"#include "inc/hw_types.h"#include "inc/hw_ints.h"#include "driverlib/sysctl.h"#include "driverlib/adc.h"#include "driverlib/interrupt.h"#include "driverlib/gpio.h"#include "driverlib/pin_map.h"#include "grlib/grLib.h"#include "grlib/grLibDriver.h"
short sample[8];
#pragma vector=unused_interruptsinterrupt void user_trap_function(void) //ISR to handle the end of sampling interrup, being the only enabled interrupt{ int df = 15625; //fs/N = 125000/8 int Re[8]; int Im[8]; int Ampl[8]; int fr[8]; int N = 8; //number of samples x[N] = (short)sample; // convert sample values to short integers for computation int out[2] = {0,0}; //init Re and Im results int j=0; for (j = 0; j < N; j++){ dft(x,j,out); //call DFT function Re[j] = out[0]; Im[j] = out[1]; //collect real and imaginary parts Ampl[j] = ((Re[j]^2)+(Im[j]^2))^(1/2); fr[j] = df*j;
long lX1 = (long)Ampl[j]; long lX2 = (long)Ampl[j]+1; long lY = (long)fr[j]; long ulValue = 128;
void LineDrawH (pvDisplayData, lX1, , lX1, lY, ulValue);
}
int M = 0;unsigned long sample[8]
void LineDrawH (void *pvDisplayData, long lX1, long lX2, long lY, unsigned long ulValue);
void ADC_init( void ) { SYSCTL_RCGC0_R |= SYSCTL_RCGC0_ADC; // Enable the clock to the ADC module SYSCTL_RCGC0_R |= SYSCTL_RCGC0_ADCSPD125K; // Configure the ADC to sample at 125KS/s ADCSequenceDisable(ADC_BASE, 0); // Disable sample sequences 0 ADCSequenceConfigure(ADC_BASE, 0, ADC_TRIGGER_PROCESSOR, 1); // Configure sample sequence 0: processor trigger, priority = 1 IntPrioritySet(INT_ADC0SS0,0); // Set SS0 interrupt priority to 0 ADCSequenceStepConfigure(ADC_BASE, 0, 0, ADC_CTL_CH0); // Configure sample sequence 0 to sample external input ADCSequenceStepConfigure(ADC_BASE, 0, 1, ADC_CTL_CH0); ADCSequenceStepConfigure(ADC_BASE, 0, 2, ADC_CTL_CH0); ADCSequenceStepConfigure(ADC_BASE, 0, 3, ADC_CTL_CH0); ADCSequenceStepConfigure(ADC_BASE, 0, 4, ADC_CTL_CH0); ADCSequenceStepConfigure(ADC_BASE, 0, 5, ADC_CTL_CH0); ADCSequenceStepConfigure(ADC_BASE, 0, 6, ADC_CTL_CH0); ADCSequenceStepConfigure(ADC_BASE, 0, 7, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END); //set interrupt flag after the seventh step ADCIntEnable(ADC_BASE, 0); // Enable the interrupt for sample sequence 0 IntEnable(INT_ADC0SS0); // Enable SS0 Interupt in NVIC M+=M; // integer to detect if ADC is initialized}
unsigned long getADC0(void){
ADCProcessorTrigger(ADC0_BASE, 0); //initiate sampling while(!ADCIntStatus(ADC0_BASE, 0, false)); //monitor interrupt flag for completion of sampling ADCSequenceDataGet(ADC0_BASE, 0, sample); //assign samples to global variable, sample
return sample; //return sample to calling function}
int dft(long *x, short k, int *out) //DFT function { int sumRe = 0; //init real component int sumIm = 0; //init imaginary component int i = 0; int N = 8; float pi = 3.1416 ; float cs = 0; //init cosine component float sn = 0; //init sine component for (i = 0; i < N; i++) //for N-point DFT { cs = cos(2*pi*(k)*i/N); //real component sn = sin(2*pi*(k)*i/N); //imaginary component sumRe += x[i]*cs; //sum of real components sumIm -= x[i]*sn; //sum of imaginary components } out[0] = sumRe; //sum of real components out[1] = sumIm; //sum of imaginary components
return(out); }int main(void) { if (M>0){ ADC_init(); //initialize ADC module if not already initialized } getADC0(); //start conversion. Interrupt flag will be set after sampling and this functioned called again after ISR executes return 0;}
You used j as the loop counter and index to the output samples and related quantities in part of your program where you are doing computations involving complex numbers. When you will analyze your program and relate it to the DFT equation, your use of j can confuse or at least distract you. The use of j in cases like this is basically discouraged; since in electrical/electronics engineering, j is used as the symbol for the imaginary unit.
int j=0;for (j = 0; j < N; j++){ dft(x,j,out); //call DFT function Re[j] = out[0]; Im[j] = out[1]; //collect real and imaginary parts Ampl[j] = ((Re[j]^2)+(Im[j]^2))^(1/2); fr[j] = df*j; long lX1 = (long)Ampl[j]; long lX2 = (long)Ampl[j]+1; long lY = (long)fr[j]; long ulValue = 128; void LineDrawH (pvDisplayData, lX1, , lX1, lY, ulValue);}}
int j=0;
for (j = 0; j < N; j++){
dft(x,j,out); //call DFT function
Re[j] = out[0];
Im[j] = out[1]; //collect real and imaginary parts
Ampl[j] = ((Re[j]^2)+(Im[j]^2))^(1/2);
fr[j] = df*j;
long lX1 = (long)Ampl[j];
long lX2 = (long)Ampl[j]+1;
long lY = (long)fr[j];
long ulValue = 128;
For a reason similar to the case of j, the use of i in the for block below is somewhat discouraged. In this case, i is the symbol for the imaginary unit in mathematics.
for (i = 0; i < N; i++) //for N-point DFT { cs = cos(2*pi*(k)*i/N); //real component sn = sin(2*pi*(k)*i/N); //imaginary component sumRe += x[i]*cs; //sum of real components sumIm -= x[i]*sn; //sum of imaginary components }
for (i = 0; i < N; i++) //for N-point DFT
{
cs = cos(2*pi*(k)*i/N); //real component
sn = sin(2*pi*(k)*i/N); //imaginary component
sumRe += x[i]*cs; //sum of real components
sumIm -= x[i]*sn; //sum of imaginary components
so what will suggest? I change all the j to i?
As you mentioned earlier on, is there any available ready available source code that can be use?
I am looking for a set of fft/dft algorithm. A simple one that can, just so enough to allow my lm3s8962 to process the algorithm.
Since, there are quite a number of mistakes in this set of algorithm.
What is the best suggestion that you recommend me to adopt now.
In the first case you can use k instead of j, in the second case you can use n instead of i. These are the variables used in DFT equations in most references. However, you are not restricted to these suggestions. You can use anything that suits your preference and you are not limited to single-character variable name. For example inIndex or timeIdx instead of j; outIndex or freqIdx instead of i. Note that if you use n instead of i, there will be a minor conflict with N (your variable for the number of samples). Although C is case-sensitive making n and N different, you can choose to replace N with something like numOfSamples or dftLength.