Debian Codegraph @

3rdparty/iaxclient/lib/sox/resample.c

raw symbols SCIP
1 /*
2 * July 5, 1991
3 * Copyright 1991 Lance Norskog And Sundry Contributors
4 * This source code is freely redistributable and may be used for
5 * any purpose. This copyright notice must be maintained.
6 * Lance Norskog And Sundry Contributors are not responsible for
7 * the consequences of using this software.
8 */
9
10 /*
11 * Sound Tools rate change effect file.
12 * Spiffy rate changer using Smith & Wesson Bandwidth-Limited Interpolation.
13 * The algorithm is described in "Bandlimited Interpolation -
14 * Introduction and Algorithm" by Julian O. Smith III.
15 * Available on ccrma-ftp.stanford.edu as
16 * pub/BandlimitedInterpolation.eps.Z or similar.
17 *
18 * The latest stand alone version of this algorithm can be found
19 * at ftp://ccrma-ftp.stanford.edu/pub/NeXT/
20 * under the name of resample-version.number.tar.Z
21 *
22 * NOTE: There is a newer version of the resample routine then what
23 * this file was originally based on. Those adventurous might be
24 * interested in reviewing its improvesments and porting it to this
25 * version.
26 */
27
28 /* Fixed bug: roll off frequency was wrong, too high by 2 when upsampling,
29 * too low by 2 when downsampling.
30 * Andreas Wilde, 12. Feb. 1999, [email protected]
31 */
32
33 /*
34 * October 29, 1999
35 * Various changes, bugfixes(?), increased precision, by Stan Brooks.
36 *
37 * This source code is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
40 *
41 */
42 /*
43 * SJB: [11/25/99]
44 * TODO: another idea for improvement...
45 * note that upsampling usually doesn't require interpolation,
46 * therefore is faster and more accurate than downsampling.
47 * Downsampling by an integer factor is also simple, since
48 * it just involves decimation if the input is already
49 * lowpass-filtered to the output Nyquist freqency.
50 * Get the idea? :)
51 */
52
53 /* Modified to compile outside of SoX by Steve Kann <[email protected]>
54 * 14Apr2003 */
55 #define _USE_MATH_DEFINES
56 #include <math.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60
61
62 /* sox includes */
63 #include "sox.h"
64 #include "stdio.h"
65
66 /* this Float MUST match that in filter.c */
67 #define Float double/*float*/
68 #define ISCALE 0x10000
69
70 /* largest factor for which exact-coefficients upsampling will be used */
71 #define NQMAX 511
72
73 #define BUFFSIZE 8192 /*16384*/ /* Total I/O buffer size */
74
75
76 static void LpFilter(double c[],
77 long N,
78 double frq,
79 double Beta,
80 long Num);
81
82 /* makeFilter is used by filter.c */
83 int makeFilter(Float Imp[],
84 long Nwing,
85 double Froll,
86 double Beta,
87 long Num,
88 int Normalize);
89
90 static long SrcUD(resample_t r, long Nx);
91 static long SrcEX(resample_t r, long Nx);
92
93 /* define some functions/types for compatibility SK */
94 /* TODO:
95 st_sample_t was int32! (we prefer int16!)
96 Make sure that MAX/MIN work right now.
97 */
98
99
100 /*
101 * Process options
102 */
103 int st_resample_getopts(resample_t r, int n, char **argv)
104 {
105
106 /* These defaults are conservative with respect to aliasing. */
107 r->rolloff = 0.80;
108 r->beta = 16; /* anything <=2 means Nutall window */
109 r->quadr = 0;
110 r->Nmult = 45;
111
112 /* This used to fail, but with sox-12.15 it works. AW */
113 if ((n >= 1)) {
114 if (!strcmp(argv[0], "-qs")) {
115 r->quadr = 1;
116 n--; argv++;
117 }
118 else if (!strcmp(argv[0], "-q")) {
119 r->rolloff = 0.875;
120 r->quadr = 1;
121 r->Nmult = 75;
122 n--; argv++;
123 }
124 else if (!strcmp(argv[0], "-ql")) {
125 r->rolloff = 0.94;
126 r->quadr = 1;
127 r->Nmult = 149;
128 n--; argv++;
129 }
130 }
131
132 if ((n >= 1) && (sscanf(argv[0], "%lf", &r->rolloff) != 1))
133 {
134 st_fail("Usage: resample [ rolloff [ beta ] ]");
135 return (ST_EOF);
136 }
137 else if ((r->rolloff <= 0.01) || (r->rolloff >= 1.0))
138 {
139 st_fail("resample: rolloff factor (%f) no good, should be 0.01<x<1.0", r->rolloff);
140 return(ST_EOF);
141 }
142
143 if ((n >= 2) && !sscanf(argv[1], "%lf", &r->beta))
144 {
145 st_fail("Usage: resample [ rolloff [ beta ] ]");
146 return (ST_EOF);
147 }
148 else if (r->beta <= 2.0) {
149 r->beta = 0;
150 st_report("resample opts: Nuttall window, cutoff %f\n", r->rolloff);
151 } else {
152 st_report("resample opts: Kaiser window, cutoff %f, beta %f\n", r->rolloff, r->beta);
153 }
154 return (ST_SUCCESS);
155 }
156
157 /*
158 * Prepare processing.
159 */
160 int st_resample_start(resample_t *rH, int inrate, int outrate)
161 {
162 long Xoff, gcdrate;
163 int i;
164 resample_t r;
165
166 *rH = malloc(sizeof (struct resamplestuff));
167 r=*rH;
168
169 if(!r)
170 {
171 st_fail("can't allocate memory");
172 }
173
174 /* just set defaults */
175 st_resample_getopts(r,0,NULL);
176
177 if (inrate == outrate)
178 {
179 st_fail("Input and Output rates must be different to use resample effect");
180 return(ST_EOF);
181 }
182
183 r->Factor = (double)outrate / (double)inrate;
184
185 gcdrate = st_gcd((long)inrate, (long)outrate);
186 r->a = inrate / gcdrate;
187 r->b = outrate / gcdrate;
188
189 if (r->a <= r->b && r->b <= NQMAX) {
190 r->quadr = -1; /* exact coeff's */
191 r->Nq = r->b; /* MAX(r->a,r->b); */
192 } else {
193 r->Nq = Nc; /* for now */
194 }
195
196 /* Check for illegal constants */
197 # if 0
198 if (Lp >= 16) st_fail("Error: Lp>=16");
199 if (Nb+Nhg+NLpScl >= 32) st_fail("Error: Nb+Nhg+NLpScl>=32");
200 if (Nh+Nb > 32) st_fail("Error: Nh+Nb>32");
201 # endif
202
203 /* Nwing: # of filter coeffs in right wing */
204 r->Nwing = r->Nq * (r->Nmult/2+1) + 1;
205
206 r->Imp = (Float *)malloc(sizeof(Float) * (r->Nwing+2)) + 1;
207 /* need Imp[-1] and Imp[Nwing] for quadratic interpolation */
208 /* returns error # <=0, or adjusted wing-len > 0 */
209 i = makeFilter(r->Imp, r->Nwing, r->rolloff, r->beta, r->Nq, 1);
210 if (i <= 0)
211 {
212 st_fail("resample: Unable to make filter\n");
213 return (ST_EOF);
214 }
215
216 /*st_report("Nmult: %ld, Nwing: %ld, Nq: %ld\n",r->Nmult,r->Nwing,r->Nq);*/
217
218 if (r->quadr < 0) { /* exact coeff's method */
219 r->Xh = r->Nwing/r->b;
220 st_report("resample: rate ratio %ld:%ld, coeff interpolation not needed\n", r->a, r->b);
221 } else {
222 r->dhb = Np; /* Fixed-point Filter sampling-time-increment */
223 if (r->Factor<1.0) r->dhb = r->Factor*Np + 0.5;
224 r->Xh = (r->Nwing<<La)/r->dhb;
225 /* (Xh * dhb)>>La is max index into Imp[] */
226 }
227
228 /* reach of LP filter wings + some creeping room */
229 Xoff = r->Xh + 10;
230 r->Xoff = Xoff;
231
232 /* Current "now"-sample pointer for input to filter */
233 r->Xp = Xoff;
234 /* Position in input array to read into */
235 r->Xread = Xoff;
236 /* Current-time pointer for converter */
237 r->Time = Xoff;
238 if (r->quadr < 0) { /* exact coeff's method */
239 r->t = Xoff*r->Nq;
240 }
241 i = BUFFSIZE - 2*Xoff;
242 if (i < r->Factor + 1.0/r->Factor) /* Check input buffer size */
243 {
244 st_fail("Factor is too small or large for BUFFSIZE");
245 return (ST_EOF);
246 }
247
248 r->Xsize = 2*Xoff + i/(1.0+r->Factor);
249 r->Ysize = BUFFSIZE - r->Xsize;
250 /* st_report("Xsize %d, Ysize %d, Xoff %d",r->Xsize,r->Ysize,r->Xoff); */
251
252 r->X = (Float *) malloc(sizeof(Float) * (BUFFSIZE));
253 r->Y = r->X + r->Xsize;
254
255 /* Need Xoff zeros at beginning of sample */
256 for (i=0; i<Xoff; i++)
257 r->X[i] = 0;
258 return (ST_SUCCESS);
259 }
260
261 /*
262 * Processed signed long samples from ibuf to obuf.
263 * Return number of samples processed.
264 */
265 int st_resample_flow(resample_t *rH, st_sample_t *ibuf, st_sample_t *obuf,
266 st_size_t *isamp, st_size_t *osamp)
267 {
268 long i, last, Nout, Nx, Nproc;
269 resample_t r = *rH;
270
271 /* constrain amount we actually process */
272 /*fprintf(stderr,"Xp %d, Xread %d, isamp %d, ",r->Xp, r->Xread,*isamp);*/
273
274 Nproc = r->Xsize - r->Xp;
275
276 i = (r->Ysize < *osamp)? r->Ysize : *osamp;
277 if (Nproc * r->Factor >= i)
278 Nproc = i / r->Factor;
279
280 Nx = Nproc - r->Xread; /* space for right-wing future-data */
281 if (Nx <= 0)
282 {
283 st_fail("resample: Can not handle this sample rate change. Nx not positive: %d", Nx);
284 return (ST_EOF);
285 }
286 if (Nx > *isamp)
287 Nx = *isamp;
288 /*fprintf(stderr,"Nx %d\n",Nx);*/
289
290 if (ibuf == NULL) {
291 for(i = r->Xread; i < Nx + r->Xread ; i++)
292 r->X[i] = 0;
293 } else {
294 for(i = r->Xread; i < Nx + r->Xread ; i++)
295 r->X[i] = (Float)(*ibuf++)/ISCALE;
296 }
297 last = i;
298 Nproc = last - r->Xoff - r->Xp;
299
300 if (Nproc <= 0) {
301 /* fill in starting here next time */
302 r->Xread = last;
303 /* leave *isamp alone, we consumed it */
304 *osamp = 0;
305 return (ST_SUCCESS);
306 }
307 if (r->quadr < 0) { /* exact coeff's method */
308 long creep;
309 Nout = SrcEX(r, Nproc);
310 /*fprintf(stderr,"Nproc %d --> %d\n",Nproc,Nout);*/
311 /* Move converter Nproc samples back in time */
312 r->t -= Nproc * r->b;
313 /* Advance by number of samples processed */
314 r->Xp += Nproc;
315 /* Calc time accumulation in Time */
316 creep = r->t/r->b - r->Xoff;
317 if (creep)
318 {
319 r->t -= creep * r->b; /* Remove time accumulation */
320 r->Xp += creep; /* and add it to read pointer */
321 /*fprintf(stderr,"Nproc %ld, creep %ld\n",Nproc,creep);*/
322 }
323 } else { /* approx coeff's method */
324 long creep;
325 Nout = SrcUD(r, Nproc);
326 /*fprintf(stderr,"Nproc %d --> %d\n",Nproc,Nout);*/
327 /* Move converter Nproc samples back in time */
328 r->Time -= Nproc;
329 /* Advance by number of samples processed */
330 r->Xp += Nproc;
331 /* Calc time accumulation in Time */
332 creep = r->Time - r->Xoff;
333 if (creep)
334 {
335 r->Time -= creep; /* Remove time accumulation */
336 r->Xp += creep; /* and add it to read pointer */
337 /* fprintf(stderr,"Nproc %ld, creep %ld\n",Nproc,creep); */
338 }
339 }
340
341 {
342 long i,k;
343 /* Copy back portion of input signal that must be re-used */
344 k = r->Xp - r->Xoff;
345 /*fprintf(stderr,"k %d, last %d\n",k,last);*/
346 for (i=0; i<last - k; i++)
347 r->X[i] = r->X[i+k];
348
349 /* Pos in input buff to read new data into */
350 r->Xread = i;
351 r->Xp = r->Xoff;
352
353 for(i=0; i < Nout; i++) {
354 // orig: *obuf++ = r->Y[i] * ISCALE;
355 Float ftemp = r->Y[i] * ISCALE;
356
357 if (ftemp >= ST_SAMPLE_MAX)
358 *obuf = ST_SAMPLE_MAX;
359 else if (ftemp <= ST_SAMPLE_MIN)
360 *obuf = ST_SAMPLE_MIN;
361 else
362 *obuf = ftemp;
363 obuf++;
364 }
365
366 *isamp = Nx;
367 *osamp = Nout;
368
369 }
370 return (ST_SUCCESS);
371 }
372
373 /*
374 * Process tail of input samples.
375 */
376 int st_resample_drain(resample_t *rH, st_sample_t *obuf, st_size_t *osamp)
377 {
378 long isamp_res, osamp_res;
379 st_sample_t *Obuf;
380 int rc;
381 resample_t r = *rH;
382
383 /* fprintf(stderr,"Xoff %d, Xt %d <--- DRAIN\n",r->Xoff, r->Xt); */
384
385 /* stuff end with Xoff zeros */
386 isamp_res = r->Xoff;
387 osamp_res = *osamp;
388 Obuf = obuf;
389 while (isamp_res>0 && osamp_res>0) {
390 st_sample_t Isamp, Osamp;
391 Isamp = isamp_res;
392 Osamp = osamp_res;
393 rc = st_resample_flow(rH, NULL, Obuf, (st_size_t *)&Isamp, (st_size_t *)&Osamp);
394 if (rc)
395 return rc;
396 /* fprintf(stderr,"DRAIN isamp,osamp (%d,%d) -> (%d,%d)\n",
397 isamp_res,osamp_res,Isamp,Osamp); */
398 Obuf += Osamp;
399 osamp_res -= Osamp;
400 isamp_res -= Isamp;
401 }
402 *osamp -= osamp_res;
403 /* fprintf(stderr,"DRAIN osamp %d\n", *osamp); */
404 if (isamp_res)
405 st_warn("drain overran obuf by %d\n", isamp_res);
406 return (ST_SUCCESS);
407 }
408
409 /*
410 * Do anything required when you stop reading samples.
411 * Don't close input file!
412 */
413 int st_resample_stop(resample_t *rH)
414 {
415 resample_t r = *rH;
416
417 free(r->Imp - 1);
418 free(r->X);
419 free(r);
420 /* free(r->Y); Y is in same block starting at X */
421 return (ST_SUCCESS);
422 }
423
424 /* over 90% of CPU time spent in this iprodUD() function */
425 /* quadratic interpolation */
426 static double qprodUD(const Float Imp[], const Float *Xp, long Inc, double T0,
427 long dhb, long ct)
428 {
429 const double f = 1.0/(1<<La);
430 double v;
431 long Ho;
432
433 Ho = T0 * dhb;
434 Ho += (ct-1)*dhb; /* so Float sum starts with smallest coef's */
435 Xp += (ct-1)*Inc;
436 v = 0;
437 do {
438 Float coef;
439 long Hoh;
440 Hoh = Ho>>La;
441 coef = Imp[Hoh];
442 {
443 Float dm,dp,t;
444 dm = coef - Imp[Hoh-1];
445 dp = Imp[Hoh+1] - coef;
446 t =(Ho & Amask) * f;
447 coef += ((dp-dm)*t + (dp+dm))*t*0.5;
448 }
449 /* filter coef, lower La bits by quadratic interpolation */
450 v += coef * *Xp; /* sum coeff * input sample */
451 Xp -= Inc; /* Input signal step. NO CHECK ON ARRAY BOUNDS */
452 Ho -= dhb; /* IR step */
453 } while(--ct);
454 return v;
455 }
456
457 /* linear interpolation */
458 static double iprodUD(const Float Imp[], const Float *Xp, long Inc,
459 double T0, long dhb, long ct)
460 {
461 const double f = 1.0/(1<<La);
462 double v;
463 long Ho;
464
465 Ho = T0 * dhb;
466 Ho += (ct-1)*dhb; /* so Float sum starts with smallest coef's */
467 Xp += (ct-1)*Inc;
468 v = 0;
469 do {
470 Float coef;
471 long Hoh;
472 Hoh = Ho>>La;
473 /* if (Hoh >= End) break; */
474 coef = Imp[Hoh] + (Imp[Hoh+1]-Imp[Hoh]) * (Ho & Amask) * f;
475 /* filter coef, lower La bits by linear interpolation */
476 v += coef * *Xp; /* sum coeff * input sample */
477 Xp -= Inc; /* Input signal step. NO CHECK ON ARRAY BOUNDS */
478 Ho -= dhb; /* IR step */
479 } while(--ct);
480 return v;
481 }
482
483 /* From resample:filters.c */
484 /* Sampling rate conversion subroutine */
485
486 static long SrcUD(resample_t r, long Nx)
487 {
488 Float *Ystart, *Y;
489 double Factor;
490 double dt; /* Step through input signal */
491 double time;
492 double (*prodUD)();
493 int n;
494
495 prodUD = (r->quadr)? qprodUD:iprodUD; /* quadratic or linear interp */
496 Factor = r->Factor;
497 time = r->Time;
498 dt = 1.0/Factor; /* Output sampling period */
499 /*fprintf(stderr,"Factor %f, dt %f, ",Factor,dt); */
500 /*fprintf(stderr,"Time %f, ",r->Time);*/
501 /* (Xh * dhb)>>La is max index into Imp[] */
502 /*fprintf(stderr,"ct=%d\n",ct);*/
503 /*fprintf(stderr,"ct=%.2f %d\n",(double)r->Nwing*Na/r->dhb, r->Xh);*/
504 /*fprintf(stderr,"ct=%ld, T=%.6f, dhb=%6f, dt=%.6f\n",
505 r->Xh, time-floor(time),(double)r->dhb/Na,dt);*/
506 Ystart = Y = r->Y;
507 n = (int)ceil((double)Nx/dt);
508 while(n--)
509 {
510 Float *Xp;
511 double v;
512 double T;
513 T = time-floor(time); /* fractional part of Time */
514 Xp = r->X + (long)time; /* Ptr to current input sample */
515
516 /* Past inner product: */
517 v = (*prodUD)(r->Imp, Xp, -1, T, r->dhb, r->Xh); /* needs Np*Nmult in 31 bits */
518 /* Future inner product: */
519 v += (*prodUD)(r->Imp, Xp+1, 1, (1.0-T), r->dhb, r->Xh); /* prefer even total */
520
521 if (Factor < 1) v *= Factor;
522 *Y++ = v; /* Deposit output */
523 time += dt; /* Move to next sample by time increment */
524 }
525 r->Time = time;
526 /*fprintf(stderr,"Time %f\n",r->Time);*/
527 return (Y - Ystart); /* Return the number of output samples */
528 }
529
530 /* exact coeff's */
531 static double prodEX(const Float Imp[], const Float *Xp,
532 long Inc, long T0, long dhb, long ct)
533 {
534 double v;
535 const Float *Cp;
536
537 Cp = Imp + (ct-1)*dhb + T0; /* so Float sum starts with smallest coef's */
538 Xp += (ct-1)*Inc;
539 v = 0;
540 do {
541 v += *Cp * *Xp; /* sum coeff * input sample */
542 Cp -= dhb; /* IR step */
543 Xp -= Inc; /* Input signal step. */
544 } while(--ct);
545 return v;
546 }
547
548 static long SrcEX(resample_t r, long Nx)
549 {
550 Float *Ystart, *Y;
551 double Factor;
552 long a,b;
553 long time;
554 int n;
555
556 Factor = r->Factor;
557 time = r->t;
558 a = r->a;
559 b = r->b;
560 Ystart = Y = r->Y;
561 n = (Nx*b + (a-1))/a;
562 while(n--)
563 {
564 Float *Xp;
565 double v;
566 long T;
567 T = time % b; /* fractional part of Time */
568 Xp = r->X + (time/b); /* Ptr to current input sample */
569
570 /* Past inner product: */
571 v = prodEX(r->Imp, Xp, -1, T, b, r->Xh);
572 /* Future inner product: */
573 v += prodEX(r->Imp, Xp+1, 1, b-T, b, r->Xh);
574
575 if (Factor < 1) v *= Factor;
576 *Y++ = v; /* Deposit output */
577 time += a; /* Move to next sample by time increment */
578 }
579 r->t = time;
580 return (Y - Ystart); /* Return the number of output samples */
581 }
582
583 int makeFilter(Float Imp[], long Nwing, double Froll, double Beta,
584 long Num, int Normalize)
585 {
586 double *ImpR;
587 long Mwing, i;
588
589 if (Nwing > MAXNWING) /* Check for valid parameters */
590 return(-1);
591 if ((Froll<=0) || (Froll>1))
592 return(-2);
593
594 /* it does help accuracy a bit to have the window stop at
595 * a zero-crossing of the sinc function */
596 Mwing = floor((double)Nwing/(Num/Froll))*(Num/Froll) +0.5;
597 if (Mwing==0)
598 return(-4);
599
600 ImpR = (double *) malloc(sizeof(double) * Mwing);
601
602 /* Design a Nuttall or Kaiser windowed Sinc low-pass filter */
603 LpFilter(ImpR, Mwing, Froll, Beta, Num);
604
605 if (Normalize) { /* 'correct' the DC gain of the lowpass filter */
606 long Dh;
607 double DCgain;
608 DCgain = 0;
609 Dh = Num; /* Filter sampling period for factors>=1 */
610 for (i=Dh; i<Mwing; i+=Dh)
611 DCgain += ImpR[i];
612 DCgain = 2*DCgain + ImpR[0]; /* DC gain of real coefficients */
613 /*st_report("DCgain err=%.12f",DCgain-1.0);*/
614
615 DCgain = 1.0/DCgain;
616 for (i=0; i<Mwing; i++)
617 Imp[i] = ImpR[i]*DCgain;
618
619 } else {
620 for (i=0; i<Mwing; i++)
621 Imp[i] = ImpR[i];
622 }
623 free(ImpR);
624 for (i=Mwing; i<=Nwing; i++) Imp[i] = 0;
625 /* Imp[Mwing] and Imp[-1] needed for quadratic interpolation */
626 Imp[-1] = Imp[1];
627
628 return(Mwing);
629 }
630
631 /* LpFilter()
632 *
633 * reference: "Digital Filters, 2nd edition"
634 * R.W. Hamming, pp. 178-179
635 *
636 * Izero() computes the 0th order modified bessel function of the first kind.
637 * (Needed to compute Kaiser window).
638 *
639 * LpFilter() computes the coeffs of a Kaiser-windowed low pass filter with
640 * the following characteristics:
641 *
642 * c[] = array in which to store computed coeffs
643 * frq = roll-off frequency of filter
644 * N = Half the window length in number of coeffs
645 * Beta = parameter of Kaiser window
646 * Num = number of coeffs before 1/frq
647 *
648 * Beta trades the rejection of the lowpass filter against the transition
649 * width from passband to stopband. Larger Beta means a slower
650 * transition and greater stopband rejection. See Rabiner and Gold
651 * (Theory and Application of DSP) under Kaiser windows for more about
652 * Beta. The following table from Rabiner and Gold gives some feel
653 * for the effect of Beta:
654 *
655 * All ripples in dB, width of transition band = D*N where N = window length
656 *
657 * BETA D PB RIP SB RIP
658 * 2.120 1.50 +-0.27 -30
659 * 3.384 2.23 0.0864 -40
660 * 4.538 2.93 0.0274 -50
661 * 5.658 3.62 0.00868 -60
662 * 6.764 4.32 0.00275 -70
663 * 7.865 5.0 0.000868 -80
664 * 8.960 5.7 0.000275 -90
665 * 10.056 6.4 0.000087 -100
666 */
667
668
669 #define IzeroEPSILON 1E-21 /* Max error acceptable in Izero */
670
671 static double Izero(double x)
672 {
673 double sum, u, halfx, temp;
674 long n;
675
676 sum = u = n = 1;
677 halfx = x/2.0;
678 do {
679 temp = halfx/(double)n;
680 n += 1;
681 temp *= temp;
682 u *= temp;
683 sum += u;
684 } while (u >= IzeroEPSILON*sum);
685 return(sum);
686 }
687
688 static void LpFilter(double *c, long N, double frq, double Beta, long Num)
689 {
690 long i;
691
692 /* Calculate filter coeffs: */
693 c[0] = frq;
694 for (i=1; i<N; i++) {
695 double x = M_PI*(double)i/(double)(Num);
696 c[i] = sin(x*frq)/x;
697 }
698
699 if (Beta>2) { /* Apply Kaiser window to filter coeffs: */
700 double IBeta = 1.0/Izero(Beta);
701 for (i=1; i<N; i++) {
702 double x = (double)i / (double)(N);
703 c[i] *= Izero(Beta*sqrt(1.0-x*x)) * IBeta;
704 }
705 } else { /* Apply Nuttall window: */
706 for(i = 0; i < N; i++) {
707 double x = M_PI*i / N;
708 c[i] *= 0.36335819 + 0.4891775*cos(x) + 0.1365995*cos(2*x) + 0.0106411*cos(3*x);
709 }
710 }
711 }