Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(){
- FILE *textFile;
- textFile=fopen("signals.txt");
- int N,M,readReturn;
- int r=0;
- int c=0;
- fscanf(textFile,"%d %d",&N,&M);
- double ar[N*M];
- int i=0;
- for(i=0;i<N*M;i++){
- fscanf(textFile,"%f",&ar[i]);
- }
- double ansAr[N*N];
- double rowAverage[N];
- double rowStandardDeviation[N];
- for(r=0;r<N;r++){
- double rowSum=0;
- for(c=0;c<M;c++){
- rowSum+=ar[(r*M)+c];
- }
- rowAverage[r]=rowSum/M;
- double rowDeviationSquared=0;
- for(c=0;c<M;c++){
- rowDeviationSquared+=square(ar[(r*M)+c]-rowAverage[r]);
- }
- rowStandardDeviation[r]=sqrt(rowDeviationSquared/M);
- }
- for(r=0;r<N;r++){
- for(c=r+1;c<N;c++){
- double eSum=0;
- int i=0;
- for(i=0;i<M;i++){
- eSum+=(ar[r*M+i]-rowAverage[r])*(ar[c*M+i]-rowAverage[c]);
- }
- ansAr[(r*N)+c]=eSum/(M*rowStandardDeviation[r]*rowStandardDeviation[c]);
- }
- }
- for(r=0;r<N;r++)ansAr[(r*N)+r]=1.0;
- for(r=0;r<N;r++){
- for(c=0;c<r;c++){
- ansAr[(r*N)+c]=ansAr[(c*N)+r];
- }
- }
- int indexMaxR=-1;
- int indexMaxC=-1;
- int max=-100;
- for(r=0;r<N;r++){
- for(c=r+1;c<N;c++){
- if(max<ansAr[(r*N)+c]){
- max=ansAr[(r*N)+c];
- indexMaxR=r;
- indexMaxC=c;
- }
- }
- }
- FILE *writeFile;
- writeFile=fopen("correlation.txt","w+");
- i=0;
- for(r=0;r<N;r++){
- for(c=0;c<N;c++){
- fputs("%.2f ",ansAr[r*N + c]);
- }
- fputs("\n");
- }
- i=0;
- for(i=0;i<M;i++) fputs("%.2f ",ar[indexMaxR*M + i]);
- fputs("\n");
- for(i=0;i<M;i++) fputs("%.2f ",ar[indexMaxC*M + i]);
- fputs("\n");
- fclose(textFile);
- fclose(writeFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement