Subversion Repositories svnkaklik

Rev

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

Rev 561 Rev 562
Line 21... Line 21...
21
#define MAX_RANGE	10.0	// maximal working radius in meters
21
#define MAX_RANGE	10.0	// maximal working radius in meters
22
 
22
 
23
static char *device = "plughw:0,0";			/* playback device */
23
static char *device = "plughw:0,0";			/* playback device */
24
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
24
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
25
static unsigned int rate = 96000;			/* stream rate */
25
static unsigned int rate = 96000;			/* stream rate */
26
static unsigned int buffer_time = MAX_RANGE / SOUND_SPEED * 1e6;		/* ring buffer length in us */
26
static unsigned int buffer_time = 2 * (MAX_RANGE / SOUND_SPEED * 1e6);		/* ring buffer length in us */
27
static unsigned int period_time = MAX_RANGE / SOUND_SPEED * 1e5;		/* period time in us */
27
static unsigned int period_time = MAX_RANGE / SOUND_SPEED * 1e6;		/* period time in us */
28
static int resample = 1;				/* enable alsa-lib resampling */
28
static int resample = 1;				/* enable alsa-lib resampling */
29
 
29
 
30
#define SIGNAL_SAMPLES 100000
-
 
31
 
-
 
32
unsigned int chirp_size;
30
unsigned int chirp_size;
33
 
31
 
34
int period=0;
-
 
35
int cperiod=0;
-
 
36
short *chirp;
-
 
37
short signal[1000000];		// record 6s of input samples
-
 
38
 
-
 
39
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
32
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
40
static snd_pcm_sframes_t period_size;	//samples per frame
33
static snd_pcm_sframes_t period_size;	//samples per frame
41
static snd_output_t *output = NULL;
34
static snd_output_t *output = NULL;
42
 
35
 
43
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels)
36
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels)
Line 173... Line 166...
173
        return err;
166
        return err;
174
    }
167
    }
175
    return 0;
168
    return 0;
176
}
169
}
177
 
170
 
178
struct async_private_data
-
 
179
{
-
 
180
    signed short *samples;
-
 
181
    snd_pcm_channel_area_t *areas;
-
 
182
    unsigned int period;
-
 
183
};
-
 
184
 
-
 
185
 
-
 
186
////// SIGNAL GENERATION STUFF
171
////// SIGNAL GENERATION STUFF
187
unsigned int linear_windowed_chirp(short *pole)
172
unsigned int linear_windowed_chirp(short *pole)
188
{
173
{
189
    unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
174
    unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
190
 
175
 
191
    static const float f0 = 1000;		//starting frequency
176
    static const float f0 = 5000;		//starting frequency
192
    static const float fmax = 7000;		//ending frequency
177
    static const float fmax = 13000;		//ending frequency
193
    static const float Tw = 0.002;
178
    static const float Tw = 0.0015;
194
    static float k;
179
    static float k;
195
 
180
 
196
    unsigned int n=0;
181
    unsigned int n=0;
197
    double t;
182
    double t;
198
    unsigned int chirp_samples;		// number of samples per period
183
    unsigned int chirp_samples;		// number of samples per period
Line 206... Line 191...
206
        pole[n] = (short) floor( (0.35875 - 0.48829*cos(2*M_PI*t*1/Tw) + 0.14128*cos(2*M_PI*2*t*1/Tw) - 0.01168*cos(2*M_PI*3*t*1/Tw))*maxval*sin(2*M_PI*(t)*(f0+(k/2)*(t))) );
191
        pole[n] = (short) floor( (0.35875 - 0.48829*cos(2*M_PI*t*1/Tw) + 0.14128*cos(2*M_PI*2*t*1/Tw) - 0.01168*cos(2*M_PI*3*t*1/Tw))*maxval*sin(2*M_PI*(t)*(f0+(k/2)*(t))) );
207
    }
192
    }
208
    return (chirp_samples);
193
    return (chirp_samples);
209
}
194
}
210
 
195
 
211
/////////// CALL BACK STUFF ///////////////////
-
 
212
static void async_playback_callback(snd_async_handler_t *ahandler)
-
 
213
{
-
 
214
    snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
-
 
215
    snd_pcm_sframes_t avail;
-
 
216
    int err;
-
 
217
 
-
 
218
    avail = snd_pcm_avail_update(handle);
-
 
219
    while ((avail >= period_size) && ((period*period_size) < chirp_size) )
-
 
220
    {
-
 
221
 
-
 
222
        err = snd_pcm_writei(handle, (chirp+period*period_size), period_size);
-
 
223
        if (err < 0)
-
 
224
        {
-
 
225
            printf("Write error: %s\n", snd_strerror(err));
-
 
226
            exit(EXIT_FAILURE);
-
 
227
        }
-
 
228
        if (err != period_size)
-
 
229
        {
-
 
230
            printf("Write error: written %i expected %li\n", err, period_size);
-
 
231
            exit(EXIT_FAILURE);
-
 
232
        }
-
 
233
        avail = snd_pcm_avail_update(handle);
-
 
234
        period++;
-
 
235
    }
-
 
236
}
-
 
237
 
-
 
238
static void async_capture_callback(snd_async_handler_t *ahandler)
-
 
239
{
-
 
240
    snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
-
 
241
    snd_pcm_sframes_t avail;
-
 
242
    int err;
-
 
243
 
-
 
244
    avail = snd_pcm_avail_update(handle);
-
 
245
    while ((avail >= period_size) /*&& ((period*period_size) < (CHIRP_SIZE-100))*/ )    // segmentation fault checking disabled
-
 
246
    {
-
 
247
 
-
 
248
        err = snd_pcm_readi(handle, (signal+cperiod*period_size), period_size);
-
 
249
        if (err < 0)
-
 
250
        {
-
 
251
            printf("Read error: %s\n", snd_strerror(err));
-
 
252
            exit(EXIT_FAILURE);
-
 
253
        }
-
 
254
        if (err != period_size)
-
 
255
        {
-
 
256
            printf("Read error: red %i expected %li\n", err, period_size);
-
 
257
            exit(EXIT_FAILURE);
-
 
258
        }
-
 
259
        avail = snd_pcm_avail_update(handle);
-
 
260
        cperiod++;
-
 
261
    }
-
 
262
}
-
 
263
 
-
 
264
 
-
 
265
int main(int argc, char *argv[])
196
int main(int argc, char *argv[])
266
{
197
{
267
    snd_pcm_t *playback_handle, *capture_handle;
198
    snd_pcm_t *playback_handle, *capture_handle;
268
    int err;
199
    int err;
269
    snd_pcm_hw_params_t *hwparams;
200
    snd_pcm_hw_params_t *hwparams;
270
    snd_pcm_sw_params_t *swparams;
201
    snd_pcm_sw_params_t *swparams;
271
    signed short *frame;  // pointer to array of samples
-
 
272
    unsigned int chn;
-
 
273
    snd_pcm_channel_area_t *areas;
-
 
274
 
202
 
275
    struct async_private_data data;
203
    long int *correlationl, *correlationr;
276
    snd_async_handler_t *chandler, *phandler;
204
    int *L_signal, *R_signal;
277
    int count;
205
    short *chirp, *signal;
278
    unsigned int i,j,m,n;
206
    unsigned int i,j,m,n;
279
    unsigned int delay[10];	//store delay of signifed correlation
207
    unsigned int delay[10];	//store delay of signifed correlation
280
    long int l,r;  // store correlation at strict time
208
    long int l,r;  // store correlation at strict time
281
    long int correlationl[SIGNAL_SAMPLES]; //array to store correlation curve
-
 
282
    long int correlationr[SIGNAL_SAMPLES]; //array to store correlation curve
-
 
283
    int L_signal[SIGNAL_SAMPLES];
-
 
284
    int R_signal[SIGNAL_SAMPLES];
-
 
285
 
209
 
286
    FILE *out;
210
    FILE *out;
287
 
211
 
288
    snd_pcm_hw_params_alloca(&hwparams);
212
    snd_pcm_hw_params_alloca(&hwparams);
289
    snd_pcm_sw_params_alloca(&swparams);
213
    snd_pcm_sw_params_alloca(&swparams);
Line 324... Line 248...
324
    {
248
    {
325
        printf("Setting of swparams failed: %s\n", snd_strerror(err));
249
        printf("Setting of swparams failed: %s\n", snd_strerror(err));
326
        exit(EXIT_FAILURE);
250
        exit(EXIT_FAILURE);
327
    }
251
    }
328
 
252
 
329
// generate ping pattern
-
 
330
    chirp = malloc(2*period_size * sizeof(short));
-
 
331
    chirp_size = linear_windowed_chirp(chirp);
-
 
332
 
-
 
333
// register playback callback
-
 
334
/*    err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
253
/*    err = snd_pcm_link( capture_handle, playback_handle); //link capture and playback together
335
    if (err < 0)
254
    if (err < 0)
336
    {
255
    {
337
        printf("Unable to register async handler\n");
256
        printf("Device linking error: %s\n", snd_strerror(err));
338
        exit(EXIT_FAILURE);
257
        exit(EXIT_FAILURE);
339
    }*/
258
    }*/
340
 
259
 
-
 
260
    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
-
 
262
    L_signal = malloc(period_size * sizeof(int));
-
 
263
    R_signal = malloc(period_size * sizeof(int));
-
 
264
    chirp = calloc(2*period_size, sizeof(short));
-
 
265
    signal = malloc(2*period_size * sizeof(short));
-
 
266
 
-
 
267
// generate ping pattern
341
//    for (period = 0; period < 2; period++)
268
    chirp_size = linear_windowed_chirp(chirp);
342
 
269
 
343
    err = snd_pcm_writei(playback_handle, chirp, period_size);
270
    err = snd_pcm_writei(playback_handle, chirp, period_size);
344
    if (err < 0)
271
    if (err < 0)
345
    {
272
    {
346
        printf("Initial write error: %s\n", snd_strerror(err));
273
        printf("Initial write error: %s\n", snd_strerror(err));
347
        exit(EXIT_FAILURE);
274
        exit(EXIT_FAILURE);
348
    }
275
    }
349
/*        if (err != period_size)
-
 
350
        {
-
 
351
            printf("Initial write error: written %i expected %li\n", err, period_size);
-
 
352
            exit(EXIT_FAILURE);
-
 
353
        }*/
-
 
354
 
276
 
355
// register capture callback
277
//start sream
356
/*    err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
278
    err = snd_pcm_start(playback_handle);
357
    if (err < 0)
279
    if (err < 0)
358
    {
280
    {
359
        printf("Unable to register async handler\n");
281
        printf("Start error: %s\n", snd_strerror(err));
360
        exit(EXIT_FAILURE);
282
        exit(EXIT_FAILURE);
361
    }*/
-
 
362
 
-
 
363
    snd_pcm_link(capture_handle,playback_handle); //link capture and playback together
-
 
364
 
-
 
365
//start sream
-
 
366
/*    if ((err = snd_pcm_prepare (capture_handle)) < 0)
-
 
367
    {
-
 
368
        fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
-
 
369
                 snd_strerror (err));
-
 
370
        exit (1);
-
 
371
    }
283
    }
372
    else printf("Capture device prepared...\n");*/
-
 
373
 
284
 
374
    err = snd_pcm_start(playback_handle);
285
    err = snd_pcm_start(capture_handle);
375
    if (err < 0)
286
    if (err < 0)
376
    {
287
    {
377
        printf("Start error: %s\n", snd_strerror(err));
288
        printf("Start error: %s\n", snd_strerror(err));
378
        exit(EXIT_FAILURE);
289
        exit(EXIT_FAILURE);
379
    }
290
    }
380
    else printf("Waiting for transmitt all samples\n");
291
    else printf("Waiting for transmitt all samples\n");
-
 
292
//--------------
381
 
293
 
382
    while ( snd_pcm_avail(capture_handle) < period_size )
294
    while ( snd_pcm_avail_update(capture_handle) < period_size)
383
    {
295
    {
384
        usleep(1000);
296
        usleep(1000);
385
        printf(".");
297
        printf(".");
386
    }
298
    }
387
 
299
 
-
 
300
    err = snd_pcm_drop(playback_handle);
388
    err = snd_pcm_drop(capture_handle);
301
    err = snd_pcm_drain(capture_handle);
389
    if (err < 0)
302
    if (err < 0)
390
    {
303
    {
391
        printf("Stop error: %s\n", snd_strerror(err));
304
        printf("Stop error: %s\n", snd_strerror(err));
392
        exit(EXIT_FAILURE);
305
        exit(EXIT_FAILURE);
393
    }
306
    }
394
 
307
 
-
 
308
    err = snd_pcm_readi(capture_handle, signal, period_size);
-
 
309
    if (err < 0)
-
 
310
    {
-
 
311
        printf("Read error: %s\n", snd_strerror(err));
-
 
312
        exit(EXIT_FAILURE);
-
 
313
    }
395
 
314
 
396
    j=0;
315
    j=0;
397
    for (i=0;i < SIGNAL_SAMPLES;i++)
316
    for (i=0;i < period_size;i++)		// separe inretleaved samples to two arrays
398
    {
317
    {
399
        L_signal[i]=signal[j];
318
        L_signal[i]=signal[j];
400
        R_signal[i]=signal[j+1];
319
        R_signal[i]=signal[j+1];
401
        j+=2;
320
        j+=2;
402
    }
321
    }
403
 
322
 
404
    printf("Data transmitted... \ncorrelating...\n");
323
    printf("\nData transmitted \ncorrelating\n");
405
    for (n=0; n < (SIGNAL_SAMPLES - chirp_size);n++)
324
    for (n=0; n < (period_size - chirp_size - 1); n++)
406
    {
325
    {
407
        l=0;
326
        l=0;
408
        r=0;
327
        r=0;
409
        for (m=0;m < chirp_size;m++)
328
        for (m=0;m < chirp_size;m++)
410
        {
329
        {
Line 413... Line 332...
413
        }
332
        }
414
        correlationl[n]=l;
333
        correlationl[n]=l;
415
        correlationr[n]=r;
334
        correlationr[n]=r;
416
    }
335
    }
417
 
336
 
418
    printf("Searching echos...\n");
337
    printf("Searching echos\n");
419
    r=0;
338
    r=0;
420
    l=0;
339
    l=0;
421
    for (n=0; n < (SIGNAL_SAMPLES - chirp_size);n++) 			//najde nejvetsi korelace
340
    for (n=0; n < period_size;n++) 			//najde nejvetsi korelace
422
    {
341
    {
423
        if (l < correlationl[n])
342
        if (l < correlationl[n])
424
        {
343
        {
425
            delay[1] = n;
344
            delay[1] = n;
426
            l = correlationl[n];
345
            l = correlationl[n];
Line 430... Line 349...
430
            delay[2] = n;
349
            delay[2] = n;
431
            r = correlationr[n];
350
            r = correlationr[n];
432
        }
351
        }
433
    }
352
    }
434
 
353
 
435
    printf("\nWriting output file...\n");
354
    printf("Writing output file\n");
436
    out=fopen("/tmp/sonar.txt","w");
355
    out=fopen("/tmp/sonar.txt","w");
437
    j=0;
356
    j=0;
438
    for (i=0;i<=period_size;i++)
357
    for (i=0; i <= (period_size -1); i++)
439
    {
358
    {
440
        fprintf(out,"%6d %6d %6d %6d %9ld %9ld\n",i,chirp[i],L_signal[i],R_signal[i],correlationl[i], correlationr[i]);
359
        fprintf(out,"%6d %6d %6d %6d %9ld %9ld\n",i,chirp[i],L_signal[i],R_signal[i],correlationl[i], correlationr[i]);
441
        j+=2;
360
        j+=2;
442
    }
361
    }
443
    fclose(out);
362
    fclose(out);
444
 
363
 
445
    printf("\nEcho zacina na: %d vzorku.\n", delay[1]);
364
    printf("Echo zacina na: %d vzorku.\n", delay[1]);
446
    printf("Casove na: %f s\n", ((float)delay[1]/rate));
365
    printf("Casove na: %f s\n", ((float)delay[1]/rate));
447
    printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delay[1]/rate));
366
    printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delay[1]/rate));
448
 
367
 
449
    snd_pcm_close(playback_handle);
368
    snd_pcm_close(playback_handle);
450
    snd_pcm_close(capture_handle);
369
    snd_pcm_close(capture_handle);