Subversion Repositories svnkaklik

Rev

Rev 562 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 562 Rev 563
Line 14... Line 14...
14
#include <errno.h>
14
#include <errno.h>
15
#include <getopt.h>
15
#include <getopt.h>
16
#include <alsa/asoundlib.h>
16
#include <alsa/asoundlib.h>
17
#include <sys/time.h>
17
#include <sys/time.h>
18
#include <math.h>
18
#include <math.h>
-
 
19
#include <fftw3.h>
19
 
20
 
20
#define SOUND_SPEED	340.0	// sound speed in air in metrs per second
21
#define SOUND_SPEED	340.0	// sound speed in air in metrs per second
21
#define MAX_RANGE	10.0	// maximal working radius in meters
22
#define MAX_RANGE	10.0	// maximal working radius in meters
22
 
23
 
23
static char *device = "plughw:0,0";			/* playback device */
24
static char *device = "plughw:0,0";			/* playback device */
Line 141... Line 142...
141
    if (err < 0)
142
    if (err < 0)
142
    {
143
    {
143
        printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
144
        printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
144
        return err;
145
        return err;
145
    }
146
    }
146
    // start the transfer when the buffer is almost full: never fou our case 
147
    // start the transfer when the buffer is almost full: never fou our case
147
    err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 2 * buffer_size);
148
    err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 2 * buffer_size);
148
    if (err < 0)
149
    if (err < 0)
149
    {
150
    {
150
        printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
151
        printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
151
        return err;
152
        return err;
Line 201... Line 202...
201
    snd_pcm_sw_params_t *swparams;
202
    snd_pcm_sw_params_t *swparams;
202
 
203
 
203
    long int *correlationl, *correlationr;
204
    long int *correlationl, *correlationr;
204
    int *L_signal, *R_signal;
205
    int *L_signal, *R_signal;
205
    short *chirp, *signal;
206
    short *chirp, *signal;
-
 
207
    float *chirp_spect, *echo_spect;
206
    unsigned int i,j,m,n;
208
    unsigned int i,j,m,n;
207
    unsigned int delay[10];	//store delay of signifed correlation
209
    unsigned int delayl[10],delayr[10];	//store delay of signifed correlation
208
    long int l,r;  // store correlation at strict time
210
    long int l,r;  // store correlation at strict time
-
 
211
    double df;	//frequency resolution 
-
 
212
    unsigned int frequency_bins; // number of output frequency bins 
-
 
213
 
-
 
214
    double *inchirp;
-
 
215
    fftw_complex *outchirp;
-
 
216
    fftw_plan fft_plan_chirp;
209
 
217
 
210
    FILE *out;
218
    FILE *out;
211
 
219
 
212
    snd_pcm_hw_params_alloca(&hwparams);
220
    snd_pcm_hw_params_alloca(&hwparams);
213
    snd_pcm_sw_params_alloca(&swparams);
221
    snd_pcm_sw_params_alloca(&swparams);
Line 248... Line 256...
248
    {
256
    {
249
        printf("Setting of swparams failed: %s\n", snd_strerror(err));
257
        printf("Setting of swparams failed: %s\n", snd_strerror(err));
250
        exit(EXIT_FAILURE);
258
        exit(EXIT_FAILURE);
251
    }
259
    }
252
 
260
 
253
/*    err = snd_pcm_link( capture_handle, playback_handle); //link capture and playback together
261
    /*    err = snd_pcm_link( capture_handle, playback_handle); //link capture and playback together
254
    if (err < 0)
262
        if (err < 0)
255
    {
263
        {
256
        printf("Device linking error: %s\n", snd_strerror(err));
264
            printf("Device linking error: %s\n", snd_strerror(err));
257
        exit(EXIT_FAILURE);
265
            exit(EXIT_FAILURE);
258
    }*/
266
        }*/
259
 
267
 
260
    correlationl = malloc(period_size * sizeof(long int)); //array to store correlation curve
268
    correlationl = malloc(period_size * sizeof(long int)); //array to store correlation curve
261
    correlationr = malloc(period_size * sizeof(long int)); //array to store correlation curve
269
    correlationr = malloc(period_size * sizeof(long int)); //array to store correlation curve
262
    L_signal = malloc(period_size * sizeof(int));
270
    L_signal = malloc(period_size * sizeof(int));
263
    R_signal = malloc(period_size * sizeof(int));
271
    R_signal = malloc(period_size * sizeof(int));
Line 265... Line 273...
265
    signal = malloc(2*period_size * sizeof(short));
273
    signal = malloc(2*period_size * sizeof(short));
266
 
274
 
267
// generate ping pattern
275
// generate ping pattern
268
    chirp_size = linear_windowed_chirp(chirp);
276
    chirp_size = linear_windowed_chirp(chirp);
269
 
277
 
-
 
278
    frequency_bins = chirp_size / 2 + 1;
-
 
279
    df = (double) rate / (double) chirp_size;
-
 
280
    chirp_spect = malloc(frequency_bins * sizeof(float));
-
 
281
    echo_spect = malloc(frequency_bins * sizeof(float));
-
 
282
 
-
 
283
    inchirp = fftw_malloc(sizeof(double) * chirp_size); 		// allocate input array for FFT
-
 
284
    outchirp = fftw_malloc(sizeof(fftw_complex) * frequency_bins);
-
 
285
 
-
 
286
    fft_plan_chirp = fftw_plan_dft_r2c_1d(chirp_size, inchirp, outchirp, FFTW_ESTIMATE);
-
 
287
 
-
 
288
    printf("compute chirp spectrum\n");
-
 
289
    for(i=0; i < chirp_size; i++) inchirp[i] = chirp[i];
-
 
290
    fftw_execute(fft_plan_chirp);
-
 
291
    for(i=0; i < frequency_bins; i++) chirp_spect[i] = sqrt( outchirp[i][0] * outchirp[i][0] + outchirp[i][1] * outchirp[i][1] );
-
 
292
 
-
 
293
// write chirp data to souncard buffer
270
    err = snd_pcm_writei(playback_handle, chirp, period_size);
294
    err = snd_pcm_writei(playback_handle, chirp, period_size);
271
    if (err < 0)
295
    if (err < 0)
272
    {
296
    {
273
        printf("Initial write error: %s\n", snd_strerror(err));
297
        printf("Initial write error: %s\n", snd_strerror(err));
274
        exit(EXIT_FAILURE);
298
        exit(EXIT_FAILURE);
Line 288... Line 312...
288
        printf("Start error: %s\n", snd_strerror(err));
312
        printf("Start error: %s\n", snd_strerror(err));
289
        exit(EXIT_FAILURE);
313
        exit(EXIT_FAILURE);
290
    }
314
    }
291
    else printf("Waiting for transmitt all samples\n");
315
    else printf("Waiting for transmitt all samples\n");
292
//--------------
316
//--------------
293
 
-
 
294
    while ( snd_pcm_avail_update(capture_handle) < period_size)
317
    while ( snd_pcm_avail_update(capture_handle) < period_size)
295
    {
318
    {
296
        usleep(1000);
319
        usleep(1000);
297
        printf(".");
320
        printf(".");
298
    }
321
    }
Line 323... Line 346...
323
    printf("\nData transmitted \ncorrelating\n");
346
    printf("\nData transmitted \ncorrelating\n");
324
    for (n=0; n < (period_size - chirp_size - 1); n++)
347
    for (n=0; n < (period_size - chirp_size - 1); n++)
325
    {
348
    {
326
        l=0;
349
        l=0;
327
        r=0;
350
        r=0;
328
        for (m=0;m < chirp_size;m++)
351
        for ( m = 0; m < chirp_size;m++)
329
        {
352
        {
330
            l += chirp[m]*L_signal[m+n];	// correlate with left channel
353
            l += chirp[m]*L_signal[m+n];	// correlate with left channel
331
            r += chirp[m]*R_signal[m+n];	// correlate with right channel
354
            r += chirp[m]*R_signal[m+n];	// correlate with right channel
332
        }
355
        }
333
        correlationl[n]=l;
356
        correlationl[n]=abs(l);
334
        correlationr[n]=r;
357
        correlationr[n]=abs(r);
335
    }
358
    }
336
 
359
 
337
    printf("Searching echos\n");
360
    printf("Searching echos\n");
338
    r=0;
361
    r=0;
339
    l=0;
362
    l=0;
340
    for (n=0; n < period_size;n++) 			//najde nejvetsi korelace
363
    for (n=0; n < period_size;n++) 			//najde nejvetsi korelace
341
    {
364
    {
342
        if (l < correlationl[n])
365
        if (l < correlationl[n])
343
        {
366
        {
344
            delay[1] = n;
367
            delayl[1] = n;
345
            l = correlationl[n];
368
            l = correlationl[n];
346
        }
369
        }
347
        if (r < correlationr[n])
370
        if (r < correlationr[n])
348
        {
371
        {
349
            delay[2] = n;
372
            delayr[1] = n;
350
            r = correlationr[n];
373
            r = correlationr[n];
351
        }
374
        }
352
    }
375
    }
353
 
376
 
-
 
377
    
-
 
378
 
354
    printf("Writing output file\n");
379
    printf("Writing output file\n");
355
    out=fopen("/tmp/sonar.txt","w");
380
    out=fopen("/tmp/sonar.txt","w");
356
    j=0;
381
    j=0;
357
    for (i=0; i <= (period_size -1); i++)
382
    for (i=0; i <= (period_size - 1); i++)
-
 
383
    {
-
 
384
        fprintf(out,"%6d %6d %6d %9ld %9ld\n",i,L_signal[i],R_signal[i],correlationl[i], correlationr[i]);
-
 
385
        j+=2;
-
 
386
    }
-
 
387
    fclose(out);
-
 
388
 
-
 
389
    out=fopen("/tmp/chirp.txt","w");
-
 
390
    j=0;
-
 
391
    for (i=0; i <= (chirp_size - 1); i++)
358
    {
392
    {
359
        fprintf(out,"%6d %6d %6d %6d %9ld %9ld\n",i,chirp[i],L_signal[i],R_signal[i],correlationl[i], correlationr[i]);
393
        fprintf(out,"%6d %6d %f\n", i, chirp[i], chirp_spect[i]);
360
        j+=2;
394
        j+=2;
361
    }
395
    }
362
    fclose(out);
396
    fclose(out);
363
 
397
 
364
    printf("Echo zacina na: %d vzorku.\n", delay[1]);
398
    printf("Echo zacina na: %d vzorku.\n", delayl[1]);
365
    printf("Casove na: %f s\n", ((float)delay[1]/rate));
399
    printf("Casove na: %f s\n", ((float)delayl[1]/rate));
366
    printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delay[1]/rate));
400
    printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delayl[1]/rate));
-
 
401
 
-
 
402
    free(correlationl);
-
 
403
    free(correlationr);
-
 
404
    free(L_signal);
-
 
405
    free(R_signal);
-
 
406
    free(chirp);
-
 
407
    free(signal);
367
 
408
 
368
    snd_pcm_close(playback_handle);
409
    snd_pcm_close(playback_handle);
369
    snd_pcm_close(capture_handle);
410
    snd_pcm_close(capture_handle);
370
    return 0;
411
    return 0;
371
}
412
}