Subversion Repositories svnkaklik

Rev

Rev 539 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 539 Rev 540
Line 16... Line 16...
16
#include <math.h>
16
#include <math.h>
17
 
17
 
18
static char *device = "plughw:0,0";			/* playback device */
18
static char *device = "plughw:0,0";			/* playback device */
19
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
19
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
20
static unsigned int rate = 98000;			/* stream rate */
20
static unsigned int rate = 98000;			/* stream rate */
21
static unsigned int channels = 1;			/* count of channels */
-
 
22
static unsigned int buffer_time = 500000;		/* ring buffer length in us */
21
static unsigned int buffer_time = 500000;		/* ring buffer length in us */
23
static unsigned int period_time = 100000;		/* period time in us */
22
static unsigned int period_time = 100000;		/* period time in us */
24
static int verbose = 0;					/* verbose flag */
23
static int verbose = 0;					/* verbose flag */
25
static int resample = 1;				/* enable alsa-lib resampling */
24
static int resample = 1;				/* enable alsa-lib resampling */
26
static int period_event = 0;				/* produce poll event after each period */
25
static int period_event = 0;				/* produce poll event after each period */
Line 37... Line 36...
37
 
36
 
38
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
37
static snd_pcm_sframes_t buffer_size;	// size of buffer at sound card
39
static snd_pcm_sframes_t period_size;	//samples per frame
38
static snd_pcm_sframes_t period_size;	//samples per frame
40
static snd_output_t *output = NULL;
39
static snd_output_t *output = NULL;
41
 
40
 
42
static int set_hwparams(snd_pcm_t *handle,
41
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels)
43
			snd_pcm_hw_params_t *params,
-
 
44
			snd_pcm_access_t access)
-
 
45
{
42
{
46
	unsigned int rrate;
43
	unsigned int rrate;
47
	snd_pcm_uframes_t size;
44
	snd_pcm_uframes_t size;
48
	int err, dir;
45
	int err, dir;
49
 
46
 
Line 58... Line 55...
58
	if (err < 0) {
55
	if (err < 0) {
59
		printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
56
		printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
60
		return err;
57
		return err;
61
	}
58
	}
62
	/* set the interleaved read/write format */
59
	/* set the interleaved read/write format */
63
	err = snd_pcm_hw_params_set_access(handle, params, access);
60
	err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
64
	if (err < 0) {
61
	if (err < 0) {
65
		printf("Access type not available for playback: %s\n", snd_strerror(err));
62
		printf("Access type not available for playback: %s\n", snd_strerror(err));
66
		return err;
63
		return err;
67
	}
64
	}
68
	/* set the sample format */
65
	/* set the sample format */
Line 202... Line 199...
202
  k=2*(fmax-f0)/Tw;
199
  k=2*(fmax-f0)/Tw;
203
  perioda = rate*Tw; 
200
  perioda = rate*Tw; 
204
 
201
 
205
  for(n=0;n<=perioda;n++){
202
  for(n=0;n<=perioda;n++){
206
     t = (double) n/ (double)rate;
203
     t = (double) n/ (double)rate;
207
     pole[n+offset] = (short) round ( (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))) );
204
     pole[n+offset] = (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))) );
208
  }
205
  }
209
  return (perioda+offset);
206
  return (perioda+offset);
210
}
207
}
211
 
208
 
212
// generate sine samples and store
209
// generate sine samples and store
Line 216... Line 213...
216
unsigned int n;
213
unsigned int n;
217
double t;
214
double t;
218
 
215
 
219
  for(n=0;n < delka_pole;n++){
216
  for(n=0;n < delka_pole;n++){
220
    t = 440.0 * (double) n/ (double)rate;
217
    t = 440.0 * (double) n/ (double)rate;
221
    pole[n] = (short) round(maxval*sin(2*M_PI*t));
218
    pole[n] = (short) floor(maxval*sin(2*M_PI*t));
222
  }
219
  }
223
}
220
}
224
//// generate simple sine ping
221
//// generate simple sine ping
225
unsigned int sine_ping(unsigned int *pole, unsigned int delka_pole,unsigned int offset, double frequency)
222
unsigned int sine_ping(unsigned int *pole, unsigned int delka_pole,unsigned int offset, double frequency)
226
{
223
{
Line 228... Line 225...
228
unsigned int n;
225
unsigned int n;
229
double t;
226
double t;
230
 
227
 
231
  for(n=0;n < delka_pole;n++){
228
  for(n=0;n < delka_pole;n++){
232
    t = frequency * (double) n/ (double)rate;
229
    t = frequency * (double) n/ (double)rate;
233
    pole[n] = (short) round(maxval*sin(2*M_PI*t));
230
    pole[n] = (short) floor(maxval*sin(2*M_PI*t));
234
  }
231
  }
235
}
232
}
236
 
233
 
237
/////////// CALL BACK STUFF ///////////////////
234
/////////// CALL BACK STUFF ///////////////////
238
static void async_playback_callback(snd_async_handler_t *ahandler)
235
static void async_playback_callback(snd_async_handler_t *ahandler)
Line 301... Line 298...
301
	struct async_private_data data;
298
	struct async_private_data data;
302
	snd_async_handler_t *chandler, *phandler;
299
	snd_async_handler_t *chandler, *phandler;
303
	int count;
300
	int count;
304
	unsigned int i,j,m,n;
301
	unsigned int i,j,m,n;
305
	unsigned int delay[10];	//store delay of signifed correlation
302
	unsigned int delay[10];	//store delay of signifed correlation
306
  	double r;
303
  	double l,r;  // store correlation at strict time
-
 
304
	long int correlationl[100000]; //array to store correlation curve
307
	double correlation[1000000]; //array to store correlation curve
305
	long int correlationr[100000]; //array to store correlation curve
308
 
306
 
309
	FILE *out;
307
	FILE *out;
310
 
308
 
311
	snd_pcm_hw_params_alloca(&hwparams);
309
	snd_pcm_hw_params_alloca(&hwparams);
312
	snd_pcm_sw_params_alloca(&swparams);
310
	snd_pcm_sw_params_alloca(&swparams);
313
 
311
 
-
 
312
	printf("Simple PC sonar ver. 000000001 starting work.. \n");
314
 
313
 
315
//open and set playback device
314
//open and set playback device
316
	if ((err = snd_pcm_open(&playback_handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
315
	if ((err = snd_pcm_open(&playback_handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
317
		printf("Playback open error: %s\n", snd_strerror(err));
316
		printf("Playback open error: %s\n", snd_strerror(err));
318
		return 0;
317
		return 0;
319
	}
318
	}
320
	
319
	
321
	if ((err = set_hwparams(playback_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
320
	if ((err = set_hwparams(playback_handle, hwparams, 1)) < 0) {
322
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
321
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
323
		exit(EXIT_FAILURE);
322
		exit(EXIT_FAILURE);
324
	}
323
	}
325
	if ((err = set_swparams(playback_handle, swparams)) < 0) {
324
	if ((err = set_swparams(playback_handle, swparams)) < 0) {
326
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
325
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
Line 331... Line 330...
331
	if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
330
	if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
332
		printf("Playback open error: %s\n", snd_strerror(err));
331
		printf("Playback open error: %s\n", snd_strerror(err));
333
		return 0;
332
		return 0;
334
	}
333
	}
335
	
334
	
336
	if ((err = set_hwparams(capture_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
335
	if ((err = set_hwparams(capture_handle, hwparams, 2)) < 0) {
337
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
336
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
338
		exit(EXIT_FAILURE);
337
		exit(EXIT_FAILURE);
339
	}
338
	}
340
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
339
	if ((err = set_swparams(capture_handle, swparams)) < 0) {
341
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
340
		printf("Setting of swparams failed: %s\n", snd_strerror(err));
342
		exit(EXIT_FAILURE);
341
		exit(EXIT_FAILURE);
343
	}
342
	}
344
 
343
 
345
/// generate ping pattern
344
/// generate ping pattern
346
 
345
 
347
        chirp_size=linear_windowed_chirp(chirp,1000000, 300);
346
        chirp_size=linear_windowed_chirp(chirp,1000000, 200);
348
 
347
 
349
/// register playback callback 
348
/// register playback callback 
350
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
349
	err = snd_async_add_pcm_handler(&phandler, playback_handle, async_playback_callback, &data); // fill by dummy &data
351
	if (err < 0) {
350
	if (err < 0) {
352
		printf("Unable to register async handler\n");
351
		printf("Unable to register async handler\n");
Line 402... Line 401...
402
	while(cperiod<10) {
401
	while(cperiod<10) {
403
		sleep(1);
402
		sleep(1);
404
		printf(".");
403
		printf(".");
405
	}	
404
	}	
406
	printf("\nData transmitted... \ncorrelating...\n");
405
	printf("\nData transmitted... \ncorrelating...\n");
407
 
406
	i=0;
408
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//spocita korelaci pro mozna spozdeni
407
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//we have interleaved data we must have 2 saples step
-
 
408
	  l=0;
-
 
409
          r=0;
409
	  r=0;
410
	  j=0;
-
 
411
	  for(m=0;m < chirp_size;m++)
-
 
412
          {
410
	  for(m=chirp_size;m > 0;m--) r += chirp[m]*signal[m+n];	// reverse echo patern
413
            l += chirp[m]*signal[i+j];	// correlate with left channel
-
 
414
            r += chirp[m]*signal[i+j+1];	// correlate with right channel
-
 
415
	    j+=2;
-
 
416
          }
-
 
417
	  correlationl[n]=l;
411
	  correlation[n]=abs(r);
418
	  correlationr[n]=r;
-
 
419
	  i+=2;
412
	}
420
	}
413
 
421
 
414
	printf("\nFinding echos...\n");
422
/*	printf("\nFinding echos...\n");
415
	r=0;
423
	r=0;
-
 
424
	l=0;
416
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//najde nejvetsi korelace
425
	for(n=0; n < (SIGNAL_SAMPLES - chirp_size);n++){			//najde nejvetsi korelace
417
	  if (r < correlation[n]){
426
	  if (l < correlationl[n]){
418
	  delay[1] = n;
427
	  delay[1] = n;
419
	  r = correlation[n];
428
	  l = correlationl[n];
420
	  }
429
	  }
-
 
430
	  if (r < correlationr[n]){
-
 
431
	  delay[1] = n;
-
 
432
	  r = correlationr[n];
-
 
433
	  }
421
	}
434
	}*/
422
 
435
 
423
	out=fopen("./output.txt","w");
436
	out=fopen("./output.txt","w");
-
 
437
  j=0;
-
 
438
  for(i=0;i<=100000;i++){
424
	for(i=0;i<=100000;i++) fprintf(out,"%6d	%6d %6d %6.2f\n",i,chirp[i],signal[i],correlation[i]); 
439
    fprintf(out,"%6d %6d %6d %6d %9ld %9ld\n",i,chirp[i],signal[j],signal[j+1],correlationl[i], correlationr[i]);
-
 
440
    j+=2;
-
 
441
  }
425
	fclose(out);
442
	fclose(out);
426
 
443
 
427
	printf("\nEcho zacina na: %d vzorku.\n", delay[1]);
444
	printf("\nEcho zacina na: %d vzorku.\n", delay[1]);
428
	printf("Casove na: %f s\n", ((float)delay[1]/rate));
445
	printf("Casove na: %f s\n", ((float)delay[1]/rate));
429
	printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delay[1]/rate));
446
	printf("vzdalenost: %f m\n", (SOUND_SPEED*(float)delay[1]/rate));
430
 
447
 
431
	snd_pcm_close(playback_handle);
448
	snd_pcm_close(playback_handle);
432
	snd_pcm_close(capture_handle);
449
	snd_pcm_close(capture_handle); 
433
	return 0;
450
	return 0;
434
}
451
}
435
 
452