Rev 525 | Rev 537 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download
// jednoduchy algoritmus krizove korelace
// copile gcc correlation.c -lm -o correlation
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SAMPLES 1000
#define SIGNAL_SAMPLES SAMPLES*20
int sample[SAMPLES];
int signal[SIGNAL_SAMPLES];
double correlation[SIGNAL_SAMPLES];
int obdelnik(int *pole, int delka_pole, int delka_pulsu, int spozdeni) //generuje testovaci lichobeznikovy puls do zadaneho pole
{
int i;
int value=1;
for (i=0; i <= delka_pole; i++) {
if (i < spozdeni && i < delka_pole) pole[i]=value;
if ((i > spozdeni) && (i < (spozdeni+10))) pole[i] = value++;
if (i < (spozdeni+delka_pulsu+10) && i > (spozdeni+10) && i < delka_pole) pole[i]= value;
if (i < (spozdeni+delka_pulsu+20) && i > (spozdeni+delka_pulsu+10) && i < delka_pole) pole[i] = value--;
if (i > (spozdeni+delka_pulsu+20) && i < delka_pole) pole[i]=value;
}
}
int linear_chirp(int *pole, int delka_pole, int delka_pulsu, int spozdeni){ // vygeneruje linearni chirp a vzorky ulozi do pole
static const double pi = 3.141592653589793238462643383279502884197; // Archimedes constant pi
static const float f0 = 0.01;
static const float k = 0.0001;
int t;
float ble;
if((spozdeni+delka_pulsu) < delka_pole)
for(t=0;t < delka_pulsu;t++) pole[spozdeni+t] = round ( 100*sin(2*pi*t*(f0+(k/2)*t)) );
else return 0;
}
int exp_chirp(int *pole, int delka_pole, int delka_pulsu, int spozdeni){ // vygeneruje linearni chirp a vzorky ulozi do pole
static const double pi = 3.141592653589793238462643383279502884197; // Archimedes constant pi
static const float f0 = 0.01;
static const float k = 0.0001;
int t;
float ble;
if((spozdeni+delka_pulsu) < delka_pole)
for(t=0;t < delka_pulsu;t++) pole[spozdeni+t] = round ( 100*sin(2*pi*t*(f0+(k/2)*t)) );
else return 0;
}
int main (void)
{
int i,n,m,delay;
double r;
linear_chirp(sample,SAMPLES,100,0); // vyrobi vzorek signalu
linear_chirp(signal,SIGNAL_SAMPLES,500,1000); // vyrobi signal ve kterem se vzorek hleda
for(n=0; n < (SIGNAL_SAMPLES - SAMPLES);n++){ //spocita korelaci pro mozna spozdeni
r=0;
for(m=0;m < SAMPLES;m++) r += sample[m]*signal[m+n];
correlation[n]=r;
}
r=0;
for(n=0; n < (SIGNAL_SAMPLES - SAMPLES);n++){ //najde nejvetsi shodu (pro nazornost v samostatnem cyklu)
if (r < correlation[n]){
delay = n;
r = correlation[n];
}
}
for(i=0;i<SAMPLES;i++)
{
// fprintf(stdout,"%3u ",i); // vypise cislo bunky v poli (spozdeni)
fprintf(stdout,"%2i ",sample[i]);
// fprintf(stdout,"%2d ",signal[i]);
// fprintf(stdout,"%3.2f\n",correlation[i]); // vypise hodnotu korelace nejvissi cislo je nejvetsi korelace.
}
fprintf(stdout,"\nvzorek v signalu zacina na miste: %3u \n",delay);
exit(1);
}