Sunday, November 13, 2011

DSO-5200A SDK Modification FFT




Download the SDK from the Hantek Website:
DSO5200A_SDK.rar

This code was imported to Visual Studio 2008 to compile the code the MFC (microsoft foundation class) api will need to be imported from the Windows WDK. How to Download the WDK

VCDlg.cpp will need to be modified with the following code:
//start snippet        
int N=10256,M=5128;
 double data[20513],fdata[10129];
 int disp[500];
 double data1[10257];
 double data2[10257];
 double data3[10257];
 
 for(int n=0;n<N;n++){
  data1[n]=Ch1.HardwareData[n];
 }
 
 
 
 
 double data4[500];
 double ratio=10256.0/1000.0; //screen size to data size ratio
 
 

 double window[10257];
 double sum=0.0;
//////////////////Calculate the Window Function values into an array and
// calculate the sum of the input data
for(int i=0;i<N;i++){
  window[i]=0.5*(1-cos(2.0*M_PI*double(i)/double(N-1)));
  sum+=double(Ch1.HardwareData[i]);
 }

 double avg=sum/double(N);//determine average and adjust input signal
 for(int i=0;i<N;i++){data3[i]=double(Ch1.HardwareData[i])-avg;}
 
 //output to screen input multiplied by the Window Function
for(int q=0;q<10256;q++){
  m_dcImg.SelectObject(bluepen);
  m_dcImg.LineTo(int(double(q)/ratio),200+int(window[q]*data3[q]));
 }


 /////////Take Orignal Signal and Apply a Window Function////////
 for(int i=0;i<N;i++){
  data[2*i]=window[i]*data3[i]/1024.0;
  data[2*i+1]=0.0;
 }///////////////////////////////////////////////////////////////
//apply the fourier transform
four1(data,8192);
//data is complex output: c = data[j]+data[j+1]i
//calculate the amplitude of the complex data and store in fdata.
for(int j=0;j<8192;j++)fdata[j]=sqrt(data[2*j+1]*data[2*j+1]+data[2*j]*data[2*j]);
 double ratio2=4096.0/2000.0;//set ratio for frequency graph
 for(int k=0;k<2048;k++){
  
  m_dcImg.SetPixel(int(double(k)/ratio2),fdata[k],RGB(0,255,0));//draw pixels 
  m_dcImg.SelectObject(redpen);
  m_dcImg.LineTo(int(double(k)/ratio2),fdata[k]);//draw red lines
 }
//end snippet
//////////////////fourier transform algorithm///////////////////////////
////////////////////////////////////////////////////////////////////////
void four1(double* data, unsigned long nn)
{
    unsigned long n, mmax, m, j, istep, i;
    double wtemp, wr, wpr, wpi, wi, theta;
    double tempr, tempi;
 
    // reverse-binary reindexing
    n = nn<<1;
    j=1;
    for (i=1; i<n; i+=2) {
        if (j>i) {
   
            tempr=data[j-1];data[j-1]=data[i-1]; data[i-1]=tempr;
            tempr=data[j];data[j]=data[i]; data[i]=tempr;
        }
        m = nn;
        while (m>=2 && j>m) {
            j -= m;
            m >>= 1;
        }
        j += m;
    };
 
    // here begins the Danielson-Lanczos section
    mmax=2;
    while (n>mmax) {
        istep = mmax<<1;
        theta = -(2*M_PI/mmax);
        wtemp = sin(0.5*theta);
        wpr = -2.0*wtemp*wtemp;
        wpi = sin(theta);
        wr = 1.0;
        wi = 0.0;
        for (m=1; m < mmax; m += 2) {
            for (i=m; i <= n; i += istep) {
                j=i+mmax;
                tempr = wr*data[j-1] - wi*data[j];
                tempi = wr * data[j] + wi*data[j-1];
 
                data[j-1] = data[i-1] - tempr;
                data[j] = data[i] - tempi;
                data[i-1] += tempr;
                data[i] += tempi;
            }
            wtemp=wr;
            wr += wr*wpr - wi*wpi;
            wi += wi*wpr + wtemp*wpi;
        }
        mmax=istep;
    }
}



No comments:

Post a Comment