Thursday, December 31, 2015

Hanning Windowing implementation in C

The code in below read windowing coefficients from a .txt file and write windowed data to an another .txt file.
Window size and message signal sizes must be same to proper windowing.
 I use 1024 point length window. You can change it as you want :)

I generated hann coeff. using MATLAB.
The code compiled and tested in Dev-C++ 5.11 TDM-GCC.

Note: Green colored data => windowed
          Pink colored data   => input signal(sine)



 #include <stdio.h>  
 #include <stdlib.h>  
 #include <math.h>  
 #include <complex.h>  
 #include <conio.h>  
   
 float pinum;  
 typedef double complex comp;  
   
 int main(void)  
 {  
   
  FILE *sin_tab_r;  
  sin_tab_r = fopen("sin_tab_r.txt","w+") ;  
    
  FILE *hann_window_data;  
  hann_window_data = fopen("hann_window_data","w+");  
    
  FILE *hann;  
  hann = fopen("hann_coef.txt","r+");       
   
  pinum = atan2(1.0,1.0) * 4 ;  
  int Fs = 100000000; // Sampling Rate(Hz)  
  int L;  
  double time_interval = (double)1/Fs; //Sampling Period.. i.e. 1ms  
    
    
  int f1 = 1000000;  
  int f2 = 3000000;  
  int f3 = 40000000; // sine frequencies  
    
  comp sin_arr[1024];  
  double hann_coef[1024];  
  double hann_win_double[1024];  
  int hann_win_int[1024];  
    
  for(L=0;L<1024;L++)  
  {  
       fscanf(hann,"%lf",&hann_coef[L]);  // Get hann function coefficients from file
  }       
        
  for(L=0;L<1024;L++)  
  {  
      sin_arr[L] = (500 * sin(2.0 * pinum * f1 * L * time_interval ) + 700 * sin(2.0 * pinum * f3 * L * time_interval ) + (200 * sin(2.0 * pinum * f2 * L * time_interval )));  
      hann_win_double[L] = (double)((double)round(creal(sin_arr[L] ))* hann_coef[L]);  
      hann_win_int[L] = (int)hann_win_double[L];  
      fprintf(hann_window_data,"%d\n",hann_win_int[L]);  
  }  
        
        
      system("pause");  
      return 0;  
        
 }  
   

No comments:

Post a Comment