Subversion Repositories svnkaklik

Rev

Rev 524 | Rev 526 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
525 kaklik 1
// jednoduchy algoritmus krizove korelace
2
// copile gcc correlation.c -lm -o correlation
3
 
522 kaklik 4
#include <stdio.h>
5
#include <stdlib.h>
523 kaklik 6
#include <math.h>
522 kaklik 7
 
524 kaklik 8
int sample[100];
9
int signal[500];
10
 
523 kaklik 11
double correlation[1000];
522 kaklik 12
 
524 kaklik 13
int obdelnik(int *pole, int delka_pole, int delka_pulsu, int spozdeni) //generuje testovaci lichobeznikovy puls  do zadaneho pole
523 kaklik 14
{
15
int i;
16
int value=1;
524 kaklik 17
  for (i=0; i <= delka_pole; i++) { 
18
    if (i < spozdeni && i < delka_pole) pole[i]=value;
523 kaklik 19
 
20
    if ((i > spozdeni) && (i < (spozdeni+10))) pole[i] = value++;
21
 
524 kaklik 22
    if (i < (spozdeni+delka_pulsu+10) && i > (spozdeni+10) && i < delka_pole) pole[i]= value;      
523 kaklik 23
 
524 kaklik 24
    if (i < (spozdeni+delka_pulsu+20) && i > (spozdeni+delka_pulsu+10) && i < delka_pole) pole[i] = value--;      
523 kaklik 25
 
524 kaklik 26
    if (i > (spozdeni+delka_pulsu+20) && i < delka_pole) pole[i]=value;
523 kaklik 27
  }
28
}
29
 
525 kaklik 30
int linear_chirp(int *pole, int delka_pole, int delka_pulsu, int spozdeni){
31
 
32
static const double pi = 3.141592653589793238462643383279502884197; // Archimedes constant pi
33
static const float f0 = 1;
34
static const float k = 1;
35
 
36
int t;
37
 
38
//  for(t=0;t < delka_pole;t++){ pole[t] = (int) ( 100*sin(2*pi*t*(f0+(k/2)*t)) ) ;
39
//  }
40
  for(t=0;t < delka_pole;t++){ pole[t] = (int) ( 100*sin(t/2) ) ;
41
  }
42
}
43
 
522 kaklik 44
int main (void)
45
{
525 kaklik 46
   int i,n,m,delay;
524 kaklik 47
   double r;
522 kaklik 48
 
525 kaklik 49
   linear_chirp(sample,100,10,0);			// vyrobi vzorek signalu
524 kaklik 50
   obdelnik(signal,500,10,100);			// vyrobi signal ve kterem se vzorek hleda
523 kaklik 51
 
524 kaklik 52
   for(n=0; n < 400;n++){			//spocita korelaci pro mozna spozdeni
53
     r=0;
54
     for(m=0;m < 100;m++) r += sample[m]*signal[m+n];
55
     correlation[n]=r;
522 kaklik 56
   }
57
 
525 kaklik 58
   r=0;
59
   for(n=0; n < 400;n++){			//najde nejvetsi shodu (pro nazornost v samostatnem cyklu)
60
     if (r < correlation[n]){
61
       delay = n;
62
       r = correlation[n];
63
     }
64
   }
523 kaklik 65
 
525 kaklik 66
 
524 kaklik 67
   for(i=0;i<400;i++)
523 kaklik 68
   {
524 kaklik 69
     fprintf(stdout,"%3u ",i);	 		// vypise cislo bunky v poli (spozdeni)
525 kaklik 70
     fprintf(stdout,"%2d ",sample[i]);
524 kaklik 71
//     fprintf(stdout,"%2d ",signal[i]);
525 kaklik 72
//     fprintf(stdout,"%3.2f\n",correlation[i]);		// vypise hodnotu korelace nejvissi cislo je nejvetsi korelace.
523 kaklik 73
   }
525 kaklik 74
   fprintf(stdout,"\nvzorek v signalu zacina na miste: %3u \n",delay);
523 kaklik 75
 
522 kaklik 76
  exit(1);
77
}