Subversion Repositories svnkaklik

Rev

Rev 537 | 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
 
542 kaklik 14
long int correlation[SIGNAL_SAMPLES];
526 kaklik 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;
537 kaklik 19
int value=0;
524 kaklik 20
  for (i=0; i <= delka_pole; i++) { 
21
    if (i < spozdeni && i < delka_pole) pole[i]=value;
523 kaklik 22
 
542 kaklik 23
    if ((i > spozdeni) && (i <= (spozdeni+10))) pole[i] = value++;
523 kaklik 24
 
524 kaklik 25
    if (i < (spozdeni+delka_pulsu+10) && i > (spozdeni+10) && i < delka_pole) pole[i]= value;      
523 kaklik 26
 
542 kaklik 27
    if (i <= (spozdeni+delka_pulsu+19) && i > (spozdeni+delka_pulsu+9) && i < delka_pole) pole[i] = value--;      
523 kaklik 28
 
537 kaklik 29
    if (i > (spozdeni+delka_pulsu+19) && 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)
542 kaklik 42
    for(t=0;t < delka_pulsu;t++) pole[spozdeni+t] = floor( 100*sin(2*pi*t*(f0+(k/2)*t)) );
526 kaklik 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)
542 kaklik 56
    for(t=0;t < delka_pulsu;t++) pole[spozdeni+t] = floor ( 100*sin(2*pi*t*(f0+(k/2)*t)) );
526 kaklik 57
  else return 0;
58
 
59
}
60
 
61
 
522 kaklik 62
int main (void)
63
{
525 kaklik 64
   int i,n,m,delay;
542 kaklik 65
   long int r;
536 kaklik 66
	FILE *out;
522 kaklik 67
 
542 kaklik 68
   linear_chirp(sample,SAMPLES,200,0);			// vyrobi vzorek signalu
69
   linear_chirp(signal,SIGNAL_SAMPLES,200,500);			// vyrobi signal ve kterem se vzorek hleda
523 kaklik 70
 
526 kaklik 71
   for(n=0; n < (SIGNAL_SAMPLES - SAMPLES);n++){			//spocita korelaci pro mozna spozdeni
524 kaklik 72
     r=0;
526 kaklik 73
     for(m=0;m < SAMPLES;m++) r += sample[m]*signal[m+n];
524 kaklik 74
     correlation[n]=r;
522 kaklik 75
   }
76
 
525 kaklik 77
   r=0;
526 kaklik 78
   for(n=0; n < (SIGNAL_SAMPLES - SAMPLES);n++){			//najde nejvetsi shodu (pro nazornost v samostatnem cyklu)
525 kaklik 79
     if (r < correlation[n]){
80
       delay = n;
81
       r = correlation[n];
82
     }
83
   }
523 kaklik 84
 
537 kaklik 85
   out=fopen("./output.txt","w");
86
   for(i=0;i< SIGNAL_SAMPLES;i++)
523 kaklik 87
   {
526 kaklik 88
//     fprintf(stdout,"%3u ",i);	 		// vypise cislo bunky v poli (spozdeni)
542 kaklik 89
//     fprintf(stdout,"%2i ",sample[i]);
524 kaklik 90
//     fprintf(stdout,"%2d ",signal[i]);
542 kaklik 91
     fprintf(out,"%d %d %d %ld \n",i,sample[i], signal[i], correlation[i]);		// vypise hodnotu korelace nejvissi cislo je nejvetsi korelace.
523 kaklik 92
   }
525 kaklik 93
   fprintf(stdout,"\nvzorek v signalu zacina na miste: %3u \n",delay);
536 kaklik 94
	fclose(out);
522 kaklik 95
  exit(1);
96
}