Subversion Repositories svnkaklik

Rev

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];

long int 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=0;
  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+19) && i > (spozdeni+delka_pulsu+9) && i < delka_pole) pole[i] = value--;      

    if (i > (spozdeni+delka_pulsu+19) && 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] = floor( 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] = floor ( 100*sin(2*pi*t*(f0+(k/2)*t)) );
  else return 0;

}


int main (void)
{
   int i,n,m,delay;
   long int r;
        FILE *out;

   linear_chirp(sample,SAMPLES,200,0);                  // vyrobi vzorek signalu
   linear_chirp(signal,SIGNAL_SAMPLES,200,500);                 // 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];
     }
   }

   out=fopen("./output.txt","w");
   for(i=0;i< SIGNAL_SAMPLES;i++)
   {
//     fprintf(stdout,"%3u ",i);                        // vypise cislo bunky v poli (spozdeni)
//     fprintf(stdout,"%2i ",sample[i]);
//     fprintf(stdout,"%2d ",signal[i]);
     fprintf(out,"%d %d %d %ld \n",i,sample[i], signal[i], correlation[i]);             // vypise hodnotu korelace nejvissi cislo je nejvetsi korelace.
   }
   fprintf(stdout,"\nvzorek v signalu zacina na miste: %3u \n",delay);
        fclose(out);
  exit(1);
}