Subversion Repositories svnkaklik

Rev

Rev 525 | Rev 536 | 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
 
526 kaklik 8
#define SAMPLES  1000
9
#define SIGNAL_SAMPLES  SAMPLES*20
524 kaklik 10
 
526 kaklik 11
int sample[SAMPLES];
12
int signal[SIGNAL_SAMPLES];
522 kaklik 13
 
526 kaklik 14
double correlation[SIGNAL_SAMPLES];
15
 
524 kaklik 16
int obdelnik(int *pole, int delka_pole, int delka_pulsu, int spozdeni) //generuje testovaci lichobeznikovy puls  do zadaneho pole
523 kaklik 17
{
18
int i;
19
int value=1;
524 kaklik 20
  for (i=0; i <= delka_pole; i++) { 
21
    if (i < spozdeni && i < delka_pole) pole[i]=value;
523 kaklik 22
 
23
    if ((i > spozdeni) && (i < (spozdeni+10))) pole[i] = value++;
24
 
524 kaklik 25
    if (i < (spozdeni+delka_pulsu+10) && i > (spozdeni+10) && i < delka_pole) pole[i]= value;      
523 kaklik 26
 
524 kaklik 27
    if (i < (spozdeni+delka_pulsu+20) && i > (spozdeni+delka_pulsu+10) && i < delka_pole) pole[i] = value--;      
523 kaklik 28
 
524 kaklik 29
    if (i > (spozdeni+delka_pulsu+20) && i < delka_pole) pole[i]=value;
523 kaklik 30
  }
31
}
32
 
526 kaklik 33
int linear_chirp(int *pole, int delka_pole, int delka_pulsu, int spozdeni){  // vygeneruje linearni chirp a vzorky ulozi do pole
525 kaklik 34
 
35
static const double pi = 3.141592653589793238462643383279502884197; // Archimedes constant pi
526 kaklik 36
static const float f0 = 0.01;
37
static const float k = 0.0001;
525 kaklik 38
 
39
int t;
526 kaklik 40
float ble;
41
  if((spozdeni+delka_pulsu) < delka_pole)
42
    for(t=0;t < delka_pulsu;t++) pole[spozdeni+t] = round ( 100*sin(2*pi*t*(f0+(k/2)*t)) );
43
  else return 0;
525 kaklik 44
 
45
}
46
 
526 kaklik 47
int exp_chirp(int *pole, int delka_pole, int delka_pulsu, int spozdeni){  // vygeneruje linearni chirp a vzorky ulozi do pole
48
 
49
static const double pi = 3.141592653589793238462643383279502884197; // Archimedes constant pi
50
static const float f0 = 0.01;
51
static const float k = 0.0001;
52
 
53
int t;
54
float ble;
55
  if((spozdeni+delka_pulsu) < delka_pole)
56
    for(t=0;t < delka_pulsu;t++) pole[spozdeni+t] = round ( 100*sin(2*pi*t*(f0+(k/2)*t)) );
57
  else return 0;
58
 
59
}
60
 
61
 
522 kaklik 62
int main (void)
63
{
525 kaklik 64
   int i,n,m,delay;
524 kaklik 65
   double r;
522 kaklik 66
 
526 kaklik 67
   linear_chirp(sample,SAMPLES,100,0);			// vyrobi vzorek signalu
68
   linear_chirp(signal,SIGNAL_SAMPLES,500,1000);			// vyrobi signal ve kterem se vzorek hleda
523 kaklik 69
 
526 kaklik 70
   for(n=0; n < (SIGNAL_SAMPLES - SAMPLES);n++){			//spocita korelaci pro mozna spozdeni
524 kaklik 71
     r=0;
526 kaklik 72
     for(m=0;m < SAMPLES;m++) r += sample[m]*signal[m+n];
524 kaklik 73
     correlation[n]=r;
522 kaklik 74
   }
75
 
525 kaklik 76
   r=0;
526 kaklik 77
   for(n=0; n < (SIGNAL_SAMPLES - SAMPLES);n++){			//najde nejvetsi shodu (pro nazornost v samostatnem cyklu)
525 kaklik 78
     if (r < correlation[n]){
79
       delay = n;
80
       r = correlation[n];
81
     }
82
   }
523 kaklik 83
 
525 kaklik 84
 
526 kaklik 85
   for(i=0;i<SAMPLES;i++)
523 kaklik 86
   {
526 kaklik 87
//     fprintf(stdout,"%3u ",i);	 		// vypise cislo bunky v poli (spozdeni)
88
     fprintf(stdout,"%2i ",sample[i]);
524 kaklik 89
//     fprintf(stdout,"%2d ",signal[i]);
525 kaklik 90
//     fprintf(stdout,"%3.2f\n",correlation[i]);		// vypise hodnotu korelace nejvissi cislo je nejvetsi korelace.
523 kaklik 91
   }
525 kaklik 92
   fprintf(stdout,"\nvzorek v signalu zacina na miste: %3u \n",delay);
523 kaklik 93
 
522 kaklik 94
  exit(1);
95
}