Subversion Repositories svnkaklik

Rev

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

Rev 651 Rev 652
Line 18... Line 18...
18
#include <math.h>
18
#include <math.h>
19
#include <fftw3.h>
19
#include <fftw3.h>
20
 
20
 
21
#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
22
#define MAX_RANGE	10.0	// maximal working radius in meters
22
#define MAX_RANGE	10.0	// maximal working radius in meters
23
#define Xl	-0.1		// microphones position
23
//#define Xl	-0.1		// microphones position
24
#define Xr	0.1
24
//#define Xr	0.1
25
 
25
 
26
static char *device = "plughw:0,0";			/* playback device */
26
static char *device = "plughw:0,0";			/* playback device */
27
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
27
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;	/* sample format */
28
static unsigned int rate = 96000;			/* stream rate */
28
static unsigned int rate = 96000;			/* stream rate */
29
static unsigned int buffer_time = 2 * (MAX_RANGE / SOUND_SPEED * 1e6);		/* ring buffer length in us */
29
static unsigned int buffer_time = 2 * (MAX_RANGE / SOUND_SPEED * 1e6);		/* ring buffer length in us */
Line 106... Line 106...
106
    }
106
    }
107
    buffer_size = size;
107
    buffer_size = size;
108
    printf("Bufffer size set to:  %d  Requested buffer time: %ld \n", (int) buffer_size, (long) buffer_time);
108
    printf("Bufffer size set to:  %d  Requested buffer time: %ld \n", (int) buffer_size, (long) buffer_time);
109
 
109
 
110
 
110
 
111
    /// set the period time
111
    // set the period time
112
    err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
112
    err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
113
    if (err < 0)
113
    if (err < 0)
114
    {
114
    {
115
        printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
115
        printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
116
        return err;
116
        return err;
Line 170... Line 170...
170
    }
170
    }
171
    return 0;
171
    return 0;
172
}
172
}
173
 
173
 
174
////// SIGNAL GENERATION STUFF
174
////// SIGNAL GENERATION STUFF
175
unsigned int linear_windowed_chirp(short *pole)
175
unsigned int linear_windowed_chirp(short *pole)  // generate the ping signal
176
{
176
{
177
    unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
177
    unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
178
 
178
 
179
    static const float f0 = 5000;		//starting frequency
179
    static const float f0 = 5000;		//starting frequency
180
    static const float fmax = 10000;		//ending frequency
180
    static const float fmax = 10000;		//ending frequency
181
    static const float Tw = 0.0015;
181
    static const float Tw = 0.0015;	// time width of ping in seconds 
182
    static float k;
182
    static float k;
183
 
183
 
184
    unsigned int n=0;
184
    unsigned int n=0;
185
    double t;
185
    double t;
186
    unsigned int chirp_samples;		// number of samples per period
186
    unsigned int chirp_samples;		// number of samples per period
187
 
187
 
188
    k=2*(fmax-f0)/Tw;
188
    k=2*(fmax-f0)/Tw;
189
    chirp_samples = ceil(rate*Tw);
189
    chirp_samples = ceil(rate*Tw);	// compute size of ping sinal in samples
190
 
190
 
191
    for (n=0;n<=chirp_samples;n++)
191
    for (n=0;n<=chirp_samples;n++)
192
    {
192
    {
193
        t = (double) n / (double)rate;
193
        t = (double) n / (double)rate;
194
        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))) );
194
        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))) ); // signal generation formula
195
    }
195
    }
196
    return (chirp_samples);
196
    return (chirp_samples);	// return count of samples in ping
197
}
197
}
198
 
198
 
199
int main(int argc, char *argv[])
199
int main(int argc, char *argv[])
200
{
200
{
201
    snd_pcm_t *playback_handle, *capture_handle;
201
    snd_pcm_t *playback_handle, *capture_handle;
Line 206... Line 206...
206
    long int *correlationl, *correlationr;
206
    long int *correlationl, *correlationr;
207
    float *echo_map;
207
    float *echo_map;
208
    int *L_signal, *R_signal;
208
    int *L_signal, *R_signal;
209
    short *chirp, *signal;
209
    short *chirp, *signal;
210
    float *chirp_spect, *lecho_spect, *recho_spect;
210
    float *chirp_spect, *lecho_spect, *recho_spect;
211
    float a,b;
211
    float a,b, Xl, Xr;
212
    unsigned int i,j,m,n;
212
    unsigned int i,j,m,n;
213
    unsigned int delayl[10],delayr[10];	//store delay of signifed correlation
213
    unsigned int delayl[10],delayr[10];	//store delay of signifed correlation
214
    long int l,r;  // store correlation at strict time
214
    long int l,r;  // store correlation at strict time
215
    double df;	//frequency resolution 
215
    double df;	//frequency resolution 
216
    double k; // sample numbers to distance normalising constant
216
    double k; // sample numbers to distance normalising constant
217
    unsigned int frequency_bins; // number of output frequency bins 
217
    unsigned int frequency_bins; // number of output frequency bins 
218
 
218
 
219
    double *inchirp;
219
    double *inchirp;		// Fourier transform variables
220
    fftw_complex *outchirp;
220
    fftw_complex *outchirp;
221
    fftw_plan fft_plan_chirp;
221
    fftw_plan fft_plan_chirp;
222
 
222
 
223
    FILE *out;
223
    FILE *out;		// dummy variable for file data output
224
 
224
 
225
    snd_pcm_hw_params_alloca(&hwparams);
225
    snd_pcm_hw_params_alloca(&hwparams);	// allocation of soundcard parameters registers
226
    snd_pcm_sw_params_alloca(&swparams);
226
    snd_pcm_sw_params_alloca(&swparams);
227
 
227
 
228
    printf("Simple PC sonar $Rev:$ starting work.. \n");
228
    printf("Simple PC sonar $Rev:$ starting work.. \n");
229
 
229
 
230
//open and set playback device
230
//open and set playback device
Line 277... Line 277...
277
    chirp = calloc(2*period_size, sizeof(short));
277
    chirp = calloc(2*period_size, sizeof(short));
278
    signal = malloc(2*period_size * sizeof(short));
278
    signal = malloc(2*period_size * sizeof(short));
279
    echo_map = malloc(3*period_size*period_size * sizeof(float));   // Array to store two dimensional image of echos
279
    echo_map = malloc(3*period_size*period_size * sizeof(float));   // Array to store two dimensional image of echos
280
    if (echo_map == NULL) printf("Can't allocate enought memory");
280
    if (echo_map == NULL) printf("Can't allocate enought memory");
281
 
281
 
282
    k = SOUND_SPEED/rate; // normalising constant
282
    k = SOUND_SPEED/rate; // normalising constant - normalise sample number to distance
283
 
283
 
284
// generate ping pattern
284
// generate ping pattern
285
    chirp_size = linear_windowed_chirp(chirp);
285
    chirp_size = linear_windowed_chirp(chirp);
286
 
286
 
287
    frequency_bins = chirp_size / 2 + 1;
287
    frequency_bins = chirp_size / 2 + 1;
Line 365... Line 365...
365
            r += chirp[m]*R_signal[m+n];	// correlate with right channel
365
            r += chirp[m]*R_signal[m+n];	// correlate with right channel
366
        }
366
        }
367
        correlationl[n]=abs(l);
367
        correlationl[n]=abs(l);
368
        correlationr[n]=abs(r);
368
        correlationr[n]=abs(r);
369
    }
369
    }
-
 
370
Xl=-0.1;
370
 
371
Xr=0.1;
371
    m=0;
372
    m=0;
372
    printf("Building echo map\n");		// compute map from left and right correlation data
373
    printf("Building echo map\n");		// compute map from left and right correlation data
373
	for (i=0;i < period_size; i++)
374
	for (i=0;i < 200; i++)
374
	{
375
	{
375
		a=k*i;
376
		a=k*i;
376
		for(j=0;j < period_size; j++)
377
		for(j=0;j < 200; j++)
377
		{
378
		{
378
			b=k*j;
379
			b=k*j;
379
			if( (b+a) >= (Xr-Xl))
380
			if( (b+a) >= (Xr-Xl) )
380
			{
381
			{
-
 
382
//a=10;
-
 
383
//b=0;
381
				echo_map[m]=a;  ///(-a*a+b*b+Xl*Xl+Xr*Xr)/(2*Xl-2*Xr);
384
				echo_map[m]=-((a*a)-(b*b)+(Xl-Xr)*(Xl-Xr))/(2*(Xl-Xr));
382
				echo_map[m+1]=sqrt((a-b-Xl-Xr)*(a+b+Xl+Xr)*(a-b-Xl+Xr)*(a-b-Xl+Xr)*(a+b-Xl+Xr))/(-2*(Xl-Xr)*(Xl-Xr));
385
				echo_map[m+1]=sqrt((a-b-Xl-Xr)*(a+b+Xl+Xr)*(a-b-Xl+Xr)*(a-b-Xl+Xr)*(a+b-Xl+Xr)/(-2*(Xl-Xr)*(Xl-Xr)));
383
				echo_map[m+2]=correlationl[i]+correlationr[j];
386
				echo_map[m+2]=correlationl[i]+correlationr[j];
384
				m+=3;
387
				m+=3;
385
			}
388
			}
386
		}
389
		}
387
	}
390
	}
-
 
391
j=0;
-
 
392
    for (i=300;i < 700; i++) // print some debugg data from echo map
-
 
393
    {
-
 
394
	printf("% 4.3f %4.3f %6.3f\n", echo_map[j], echo_map[j+1], echo_map[j+2]);
-
 
395
	j+=3;
-
 
396
    }
-
 
397
 
388
    printf("Searching echos\n");
398
    printf("Searching echos\n");
389
    r=0;
399
    r=0;
390
    l=0;
400
    l=0;
391
    for (n=0; n < period_size;n++) 			//najde nejvetsi korelace
401
    for (n=0; n < period_size;n++) 			//najde nejvetsi korelace
392
    {
402
    {