Thank you so much Magician however, if you could help me again, it would stop my head exploding!
So, I have a project where I have designed an ADC shield which has a 12 bit ADC as well as connectors for Bluetooth, display and rtc. The idea is that the Arduino will sit there talking samples from up to 4 ADXL335 accelerometers, perform an fft, work out the overall acceleration and velocity readings which it will display on a 2004lcd, compare the readings to ISO standards for vibration and tell the user if these readings are healthy, in alert or alarm.
I have it working great on an Arduino MEGA2560 using an FFT library I found here ion the forum, but I'd like to change to a DUE and make use of the extra processing power and bigger fft sizes.
So, on the mega, the sampling routine is this
// Function fftsample(int chan, int b)
// reads samples from channel chan into global array fft_input
// b is step. use b=1 to get twf data for export
// use b=2 to get alternative twf data and zero's for fft
// ie real and imaginary data are interspersed
void fftsample(int chan, int samples, int b)
{
unsigned long start_time;
unsigned long next_time;
double twftot=0.0;
int a;
if(micros()>0xFFF0BDBF) // micros is less than 1 second until rollover
delay(1010); // 1.01 second delay to take micros past rollover
start_time = micros();
next_time = micros()+391;
for(a=0; a<samples; a+=b)
{
fft_input[a] = adc.analogRead(chan); // put real data into even bins
twftot+=fft_input[a];
fft_input[a+1] = 0; // set odd bins to 0
for(;;)
if((long)micros()>next_time)
break;
next_time+=391;
}
twfavg=(int) (twftot/(samples/b));
}
This puts the real amn imaginary values into consecutive bins.
And the fft analysis part looks like this
for(l =0; l<loops; l++)
{
vel[p]=0; // holds the latest velocity reading for point P
acc[p]=0; // holds the latest acceleration reading for point p
acum=0; // cumulateve acceleration to work out overall
vcum=0; // cumulative velocity to work out overall
fftsample(p,512,2);
for(i=0; i<512; i+=2)
fft_input[i]-=twfavg; // subtracts average values ie dc offset
fft_window(); // window the data for better frequency response
fft_reorder(); // reorder the data before doing the fft
fft_run(); // process the data in the fft
for(i=4; i<200; i+=2) Skip lower bins and calculate overall values up to 1kHz
{
fft_input[i]=absolute(fft_input[i],fft_input[i+1]);
acceleration=fft_input[i]*res*(1000/sensitivity);
acum+=(acceleration*acceleration);
velocity=(acceleration*9806.65)/(twopi*10*i);
vcum+=(velocity*velocity);
}
acc[p]=sqrt(acum/lineshape);
vel[p]=sqrt(vcum/lineshape);
if(!l) // only print to screen on first run so no flicker
{
clearlcd();
lcd.setCursor(0,0);
lcd.print(point[p]);
lcd.setCursor(0,1);
lcd.print(" Vel mms ");
lcd.setCursor(0,2);
lcd.print(" Acc g's ");
lcd.setCursor(0,3);
lcd.print(" Status ");
}
lcd.setCursor(9, 1);
if(vel[p]<10)
lcd.print(" ");
lcd.print(vel[p],2);
lcd.setCursor(9, 2);
if(acc[p]<10)
lcd.print(" ");
lcd.print(acc[p],2);
lcd.setCursor(10, 3);
if(vel[p]<=3.5)
lcd.print("Healthy");
else if(vel[p]>3.5 && vel[p]<=7.1)
lcd.print("Alert ");
else
lcd.print("ALARM ");
delay(750);
}
}
absolute is just sqrt((aa) + (bb)); ie the square route of the real squared plus the imaginary squared.
But when I try to get the same code to compile for the due, I get error messages .
So while surfing for a 'due' solution, I came across your code but even looking at the example, I'm not sure how to use it - I'm more likely to win at Wimbledon than I am to understand maths at that level! But like Marcus Willis, I do try!
So, from what I can see, there are the following functions used in your example.
radix.rev_bin( f_r, FFT_SIZE)
radix.fft_split_radix_real( f_r, LOG2_FFT)
radix.gain_Reset( f_r, LOG2_FFT -1)
radix.get_Magnit1( f_r, out1)
radix.get_Magnit2( f_r, out2)
So, lets say I call my modified fftsample() routine to put FFT_SIZE no. of samples into the variable f_r;
How do I use your functions to give me a comparable result to the functions I'm using now?
Can you please explain in 'Ball boy' terms what each function does so I and others like me, know which ones to use to get our desired results.
Thanking you so so much, Steve.