git stults online alsa-signals / master sigproc.c
master

Tree @master (Download .tar.gz)

sigproc.c @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "sigproc.h"

typedef struct filter_t
{
  size_t  n;    /* length of coefficient array */
  double *taps; /* filter coefficients */
  double  rate; /* sample rate */
  double  beta; /* rolloff factor (0, 1] */
} filter_t;

/* cdata is an array of length 2*n containing n packed complex numbers
   like cdata[0] = re[0] cdata[1] = im[0]. */
filter_t *filter_create (double rate, double beta, size_t n)
{
  size_t  i;
  size_t  mid;
  double *taps;
  double  curf;
  double  df;
  double  flb;
  double  fub;
  double  d2f;
  double  pbf;
  double  oof;

  filter_t *filter;

  /* rolloff factor is on (0,1] */
  if ((beta < 1e-15) || (beta > 1.0))
    return NULL;

  /* TODO: rate enforcement */
  if ((filter = calloc (1, sizeof(filter_t))) == NULL)
    return NULL;

  filter->n    = n;
  filter->beta = beta;
  filter->rate = rate;

  /* 
     Build filter in frequency space with frequency intervals of size
     rate/2n. To coincide with wraparound frequency notation as
     generated by fft of data, we write out positive frequencies up to
     index n/2 (frequency rate/2 or 1/2T where T = 1/rate) and then
     negative frequencies going backwards from frequency -rate/2 to
     index rate/2n at index n-1. for f = rate:

     0    1    2    3   ...  n/2    (n+1)/2  ...  n-3     n-2    n-1
     0   f/2n f/n 3f/2n      f/2     -f/2       -3f/2n   -f/n   -f/2n

     Then we build the filter coefficients for each of these bins with
     the following function:

     1/f  for |f| <= f(1-beta)/2

     (1/2f)(1 + cos(pi/(beta*f)(|f|-f(1-beta)/2))) 
          for f(1-beta)/2 < |f| <= f(1+beta)/2

     0    else

     beta controls how square the filter is on the interval
     [-f/2,+f/2]; the perfect function (beta = 0) is in fact a box,
     but this is unrelizable in practice.     
  */
  
  if ((filter->taps = calloc (n, sizeof(double))) == NULL)
    {
      free (filter);
      return NULL;
    }

  /* write positive frequencies up to mid point from left, negative
     ones from right */
  df   = rate / (2.0 * n);
  oof  = 1.0 / rate;
  flb  = rate * (1.0 - beta) / 2.0;
  fub  = rate * (1.0 + beta) / 2.0;
  d2f  = 1.0 / (2.0 * rate);
  pbf  = M_PI / (beta * rate);
  curf = 0.0;
  mid = n >> 1;
  for (i = 0; i < mid; ++i)
    {
      if (curf < flb)
	{
	  filter->taps[i        ] = oof;
	  filter->taps[n - i - 1] = oof;
	}
      else if ((curf > flb) && (curf < fub))
	{
	  double val = d2f * (1.0 + cos (pbf * (curf - flb)));
	  filter->taps[i        ] = val;
	  filter->taps[n - i - 1] = val;
	}
      /* else implicitly 0 by calloc */
      /* increment current frequency value based on bin id */
      curf += df;
    }
 
  return filter;
}

void filter_destroy (filter_t *filter)
{
  if (filter == NULL)
    return;
  free (filter->taps);
  free (filter);
  filter = NULL;
  return;
}

/* filters in-place an array of 2 * n doubles representing n packed
   complex numbers using the provided filter. The filter will only
   filter a sequence with equal length to that specified in its
   construction. */
int filter_execute (filter_t *filter, double *cdata, size_t n)
{
  size_t i;
  if (n != filter->n)
    return 1;
  fft_forward (cdata, n, 1);
  for (i = 0; i < n; ++i)
    cdata[i] *= filter->taps[i];
  fft_inverse (cdata, n, 1);
  return 0;
}

/* wrapper around gsl complex fft with error checking (TODO) and
   determination of which radix transform to use */
int fft_forward (double *data, size_t n, size_t stride)
{
  int rc;

  if (!(n & (n - 1)))
    rc = gsl_fft_complex_radix2_forward (data, stride, n);
  else
    {
      gsl_fft_complex_wavetable *wavetable;
      gsl_fft_complex_workspace *workspace;
      /* for mixed radix you need wavetable and workspace */
      wavetable = gsl_fft_complex_wavetable_alloc (n);
      workspace = gsl_fft_complex_workspace_alloc (n);
      /* can use the same workspace for ffts of the same length */
      rc = gsl_fft_complex_forward (data, stride, n, wavetable, workspace);
      /* free */
      gsl_fft_complex_wavetable_free (wavetable);
      gsl_fft_complex_workspace_free (workspace);
    }
  return 0;
}

/* wrapper around gsl complex fft with error checking (TODO) and
   determination of which radix transform to use */
int fft_inverse (double *data, size_t n, size_t stride)
{
  int rc;
  
  if (!(n & (n - 1)))
    rc = gsl_fft_complex_radix2_inverse (data, stride, n);
  else
    {
      gsl_fft_complex_wavetable *wavetable;
      gsl_fft_complex_workspace *workspace;
      /* for mixed radix you need wavetable and workspace */
      wavetable = gsl_fft_complex_wavetable_alloc (n);
      workspace = gsl_fft_complex_workspace_alloc (n);
      /* can use the same workspace for ffts of the same length */
      rc = gsl_fft_complex_inverse (data, stride, n, wavetable, workspace);
      /* free */
      gsl_fft_complex_wavetable_free (wavetable);
      gsl_fft_complex_workspace_free (workspace);
    }
  return 0;
}

double *make_pulse (double *symbols, size_t n, size_t size, size_t *ret)
{
  size_t  i;
  size_t  idx;
  double *new;
  if (symbols == NULL)
    return NULL;
  if ((new = calloc (n * size, sizeof(double))) == NULL)
    return NULL;
  idx = size >> 1;
  for (i = idx; i < n; ++i)
    new[i * size] = symbols[i];
  *ret = n * size;
  return new;
}

double *encode_bpsk (unsigned char *data, size_t n, size_t *ret)
{
  unsigned char mask;
  size_t        i;
  double       *new;
  if (data == NULL)
    return;
  if ((new = calloc (n * 8, sizeof(double))) == NULL)
    return NULL;
  for (i = 0; i < n; ++i)
    {
      mask = 0x80;
      new[8 * i    ] = (data[i] & mask) ? -1.0 : 1.0; mask >>= 1;
      new[8 * i + 1] = (data[i] & mask) ? -1.0 : 1.0; mask >>= 1;
      new[8 * i + 2] = (data[i] & mask) ? -1.0 : 1.0; mask >>= 1;
      new[8 * i + 3] = (data[i] & mask) ? -1.0 : 1.0; mask >>= 1;
      new[8 * i + 4] = (data[i] & mask) ? -1.0 : 1.0; mask >>= 1;
      new[8 * i + 5] = (data[i] & mask) ? -1.0 : 1.0; mask >>= 1;
      new[8 * i + 6] = (data[i] & mask) ? -1.0 : 1.0; mask >>= 1;
      new[8 * i + 7] = (data[i] & mask) ? -1.0 : 1.0;
    }
  *ret = n * 8;
  return new;
}

unsigned char *decode_bpsk (double *data, size_t n, size_t *ret)
{
  size_t         i;
  size_t         idx;
  unsigned char  mask;
  unsigned char *new;
  if (data == NULL)
    return NULL;
  if ((new = calloc ((n + 7) / 8, sizeof(unsigned char))) == NULL)
    return NULL;
  idx  = 0;
  mask = 0x80;
  for (i = 0; i < n; ++i)
    {
      if (data[i] < 0.0)
	new[idx] ^= mask;
      if (!(mask >>= 1))
	{
	  ++idx;
	  mask = 0x80;
	}
    }
  *ret = (n + 7) / 8;
  return new;
}

/* upsamples a complex array of length n by centering each element in
   num new elements, then smoothing the resulting array, whose length is
   returned in ret. */
double complex *interpolate (const double complex *array, size_t n,
			     size_t num, size_t *ret)
{
  size_t          i;
  size_t          idx;
  size_t          offset;
  size_t          len;
  char            even;
  double complex *up;
  double complex *filter;

  if (array == NULL)
    return NULL;

  /* each number in array will now be interpolated across num numbers */
  len = n * num;
  up = calloc (len, sizeof(double complex));

  /* figure out where the middle of each new group of num elts is;
     jump to those points and fill. if num is even, we have to split
     each element across two spaces -- do this via simple average. */
  even   = (num & 1) ? 0 : 1;
  offset = num >> 1;
  idx    = offset;
  if (even)
    for (i = 0; i < n; ++i)
      {
	double complex z = array[i] / 2.0;
	up[idx    ] = z;
	up[idx + 1] = z;
	idx += num;
      }
  else
    for (i = 0; i < n; ++i)
      {
	up[idx] = array[i];
	idx += num;
      }
  
  /* now we have to smooth the new array. do this using convolution
     with a nyquist filter */
  
  /* build filter array */
  filter = calloc (len, sizeof(double complex));
  /* adjust offset for decay length beyond sample range */
  offset += 1 /* NOT REALLY */;
  /* right lobe */
  for (i = 0; i < offset; ++i)
    ;
  /* left lobe */
  for (i = len - offset; i < len; ++i)
    ;

  /* convolve */
  fft_forward (up,     len, 1);
  fft_forward (filter, len, 1);
  
  for (i = 0; i < len; ++i)
    up[i] *= filter[i];

  free (filter);

  fft_inverse (up, len, 1);

  *ret = len;
  return up;
}

/* downsamples a complex array by extracting the central element from
   each num elements, then smoothing the resulting array, whose length
   is returned in ret */
double complex *decimate (const double complex *array, size_t n,
			  size_t num, size_t *ret)
{
  return NULL;
}

/* convolves complex arrays of length n; length of convolution is returnd in ret */
double complex *convolve (const double complex *_d1, const double complex *_d2,
			  size_t n, size_t *ret)
{
  int             i;
  int             rc1, rc2;
  double complex *d1;
  double complex *d2;
  size_t          len;
  
  /* need to pad each array with the number of zeros commensurate with
     the number of lags we want to compute (in this case, all lags);
     calloc automatically zeros the last n elements. */
  len = 2 * n;
  d1 = calloc (len, sizeof(double complex));
  d2 = calloc (len, sizeof(double complex));

  memcpy (d1, _d1, n * sizeof(double complex));
  memcpy (d2, _d2, n * sizeof(double complex));

  fft_forward (d1, n, 1);
  fft_forward (d2, n, 1);
  
  for (i = 0; i < n; ++i)
    d1[i] *= d2[i];

  free (d2);

  fft_inverse (d1, n, 1);

  *ret = n;

  return d1;
}

/* correlates complex arrays of length n; length of correlation array
   is returned in ret */
double complex *correlate (const double complex *_d1, const double complex *_d2,
			   size_t n, size_t *ret)
{
  int             i;
  int             rc1, rc2;
  double complex *d1;
  double complex *d2;
  size_t          len;
  
  /* need to pad each array with the number of zeros commensurate with
     the number of lags we want to compute (in this case, all lags);
     calloc automatically zeros the last n elements. */
  len = 2 * n;
  d1 = calloc (len, sizeof(double complex));
  d2 = calloc (len, sizeof(double complex));

  memcpy (d1, _d1, n * sizeof(double complex));
  memcpy (d2, _d2, n * sizeof(double complex));

  fft_forward (d1, n, 1);
  fft_forward (d2, n, 1);
  
  for (i = 0; i < n; i += 2)
    d1[i] *= conjf (d2[i]);
  
  free (d2);
  
  fft_inverse (d1, n, 1);
  
  *ret = n;

  return d1;
}

//void main()
//{
//  int       i;
//  filter_t *filt;
//  double   *data;
//  double    val;
//  size_t    n;
//
//  n = 1024;
//
//  data = calloc (2 * n, sizeof(double));
//  val = 1.0;
//  for (i = 0; i < n; i += 16)
//    {
//      data[2 * i]     = val;
//      data[2 * i + 1] = 0.0;
//      val *= -1.0;
//    }
//
////  for (i = 0; i < n; ++i)
////    printf ("%1.2f\n", data[2 * i]);
//
//  linquid_firdes_rcos ();
//
//  filt = filter_create (8000., 0.05, n);
//
//  for (i = 0; i < n; ++i)
//    printf ("%f\n", filt->taps[i]);
//
//  filter_execute (filt, data, n);
//
////  for (i = 0; i < n; ++i)
////    printf ("%1.2f\n", data[2 * i]);
//
//  filter_destroy (filt);
//
//  free (data);
//
//  return;
//}