I have implemented Full cycle DFT algorithm in arduino 2560. It works well with fundamental(50Hz). It attenuates all even harmonics. But surprisingly lets through all odd harmonics. What am I doing wrong? Here is my code

//Full Cycle DFT Experiment- Sampling Frequency is 1KHz and Number of samples is N=20
// Sample Mode-ADC
#define Sampling_Interval  1000  // unit us - 1KHz
#define Samples 20 
#define Scaling 3.45
const int analogInPin = A0;
long startTime;
long endTime;
uint16_t n,i;
float result;
float FCDFT(int16_t arry[]);
int16_t values[Samples];
char real_coeff[Samples];
char imag_coeff[Samples];
void setup()
{
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  startTime=0;
  endTime=0;
  n=0;
  for(i=0;i<Samples;i++)
  {
   real_coeff[i]= cos(2*3.14159*i/20);//LUT
   imag_coeff[i]= sin(2*3.14159*i/20);
  }
}

void loop() 
{
  while(1)
  { 
    startTime=micros();
    if((startTime-endTime)>=Sampling_Interval)
    {
      endTime = micros();
      if (n<Samples)
      { 
       values[n] = analogRead(analogInPin);
       n++;     
      }
      else if(n==Samples)
      {
         result= FCDFT(values);
         n=0;
         Serial.println(result*Scaling);

      }
     }     
   }
 }


float FCDFT(int16_t arry[])
{
  float real = 0;
  float imag = 0;
  float voltage =0;
  unsigned char k;
  unsigned char AVG=3;
  for (k = 0; k<Samples; k++)
  {
   voltage = arry[k] * (5.0/1023.0);
   real= real+voltage*real_coeff[k];
   imag= imag+voltage*imag_coeff[k];
  }
  return sqrt(0.01*real*real +0.01*imag*imag);
}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.