Advertisement
gihanchanaka

Untitled

Aug 29th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(){
  4.     FILE *textFile;
  5.     textFile=fopen("signals.txt");
  6.     int N,M,readReturn;
  7.     int r=0;
  8.     int c=0;
  9.  
  10.     fscanf(textFile,"%d %d",&N,&M);
  11.  
  12.     double  ar[N*M];
  13.     int i=0;
  14.     for(i=0;i<N*M;i++){
  15.         fscanf(textFile,"%f",&ar[i]);
  16.     }
  17.  
  18.     double ansAr[N*N];
  19.     double rowAverage[N];
  20.     double rowStandardDeviation[N];
  21.  
  22.     for(r=0;r<N;r++){
  23.         double rowSum=0;
  24.         for(c=0;c<M;c++){
  25.             rowSum+=ar[(r*M)+c];
  26.         }
  27.         rowAverage[r]=rowSum/M;
  28.         double rowDeviationSquared=0;
  29.         for(c=0;c<M;c++){
  30.             rowDeviationSquared+=square(ar[(r*M)+c]-rowAverage[r]);
  31.         }
  32.         rowStandardDeviation[r]=sqrt(rowDeviationSquared/M);
  33.     }
  34.  
  35.     for(r=0;r<N;r++){
  36.         for(c=r+1;c<N;c++){
  37.             double eSum=0;
  38.             int i=0;
  39.             for(i=0;i<M;i++){
  40.                 eSum+=(ar[r*M+i]-rowAverage[r])*(ar[c*M+i]-rowAverage[c]);
  41.             }
  42.             ansAr[(r*N)+c]=eSum/(M*rowStandardDeviation[r]*rowStandardDeviation[c]);
  43.         }
  44.     }
  45.     for(r=0;r<N;r++)ansAr[(r*N)+r]=1.0;
  46.     for(r=0;r<N;r++){
  47.         for(c=0;c<r;c++){
  48.             ansAr[(r*N)+c]=ansAr[(c*N)+r];
  49.         }
  50.     }
  51.  
  52.  
  53.  
  54.     int indexMaxR=-1;
  55.     int indexMaxC=-1;
  56.     int max=-100;
  57.  
  58.  
  59.     for(r=0;r<N;r++){
  60.         for(c=r+1;c<N;c++){
  61.             if(max<ansAr[(r*N)+c]){
  62.                 max=ansAr[(r*N)+c];
  63.                 indexMaxR=r;
  64.                 indexMaxC=c;
  65.             }
  66.         }
  67.     }
  68.  
  69.  
  70.     FILE *writeFile;
  71.     writeFile=fopen("correlation.txt","w+");
  72.  
  73.     i=0;
  74.  
  75.     for(r=0;r<N;r++){
  76.         for(c=0;c<N;c++){
  77.             fputs("%.2f ",ansAr[r*N + c]);
  78.         }
  79.         fputs("\n");
  80.     }
  81.  
  82.     i=0;
  83.     for(i=0;i<M;i++) fputs("%.2f ",ar[indexMaxR*M + i]);
  84.     fputs("\n");
  85.     for(i=0;i<M;i++) fputs("%.2f ",ar[indexMaxC*M + i]);
  86.     fputs("\n");
  87.  
  88.     fclose(textFile);
  89.     fclose(writeFile);
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement