forked from captainjack64/hacktv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfir.c
executable file
·779 lines (606 loc) · 17.1 KB
/
fir.c
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/* hacktv - Analogue video transmitter for the HackRF */
/*=======================================================================*/
/* Copyright 2017 Philip Heron <[email protected]> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "fir.h"
#include "common.h"
/* Some of the filter design functions contained within here where taken
from or are based on those within gnuradio's gr-filter/lib/firdes.cc */
static double i_zero(double x)
{
double sum, u, halfx, temp;
int n;
sum = u = n = 1;
halfx = x / 2.0;
do
{
temp = halfx / (double) n;
n += 1;
temp *= temp;
u *= temp;
sum += u;
}
while(u >= 1e-21 * sum);
return(sum);
}
static void kaiser(double *taps, size_t ntaps, double beta)
{
double i_beta = 1.0 / i_zero(beta);
double inm1 = 1.0 / ((double) (ntaps - 1));
double temp;
int i;
taps[0] = i_beta;
for(i = 1; i < ntaps - 1; i++)
{
temp = 2 * i * inm1 - 1;
taps[i] = i_zero(beta * sqrt(1.0 - temp * temp)) * i_beta;
}
taps[ntaps - 1] = i_beta;
}
void fir_low_pass(double *taps, size_t ntaps, double sample_rate, double cutoff, double width, double gain)
{
int n, M;
double fmax, fwT0;
/* Ensure an odd number of taps */
if((ntaps & 1) == 0)
{
ntaps -= 1;
taps[ntaps] = 0;
}
/* Create the window */
kaiser(taps, ntaps, 7.0);
/* Generate the filter taps */
M = (ntaps - 1) / 2;
fwT0 = 2.0 * M_PI * cutoff / sample_rate;
for(n = -M; n <= M; n++)
{
if(n == 0)
{
taps[n + M] *= fwT0 / M_PI;
}
else
{
taps[n + M] *= sin(n * fwT0) / (n * M_PI);
}
}
/* find the factor to normalize the gain, fmax.
* For low-pass, gain @ zero freq = 1.0 */
fmax = taps[0 + M];
for(n = 1; n <= M; n++)
{
fmax += 2 * taps[n + M];
}
/* Normalise */
gain /= fmax;
for(n = 0; n < ntaps; n++)
{
taps[n] *= gain;
}
}
void fir_band_reject(double *taps, size_t ntaps, double sample_rate, double low_cutoff, double high_cutoff, double width, double gain)
{
int n, M;
double fmax, fwT0, fwT1;
/* Ensure an odd number of taps */
if((ntaps & 1) == 0)
{
ntaps -= 1;
taps[ntaps] = 0;
}
/* Create the window */
kaiser(taps, ntaps, 7.0);
/* Generate the filter taps */
M = (ntaps - 1) / 2;
fwT0 = 2.0 * M_PI * low_cutoff / sample_rate;
fwT1 = 2.0 * M_PI * high_cutoff / sample_rate;
for(n = -M; n <= M; n++)
{
if(n == 0)
{
taps[n + M] *= 1.0 + (fwT0 - fwT1) / M_PI;
}
else
{
taps[n + M] *= (sin(n * fwT0) - sin(n * fwT1)) / (n * M_PI);
}
}
/* find the factor to normalize the gain, fmax.
* For band-reject, gain @ zero freq = 1.0 */
fmax = taps[0 + M];
for(n = 1; n <= M; n++)
{
fmax += 2 * taps[n + M];
}
/* Normalise */
gain /= fmax;
for(n = 0; n < ntaps; n++)
{
taps[n] *= gain;
}
}
void fir_complex_band_pass(double *taps, size_t ntaps, double sample_rate, double low_cutoff, double high_cutoff, double width, double gain)
{
double *lptaps;
double freq = M_PI * (high_cutoff + low_cutoff) / sample_rate;
double phase;
int i;
lptaps = &taps[ntaps];
fir_low_pass(lptaps, ntaps, sample_rate, (high_cutoff - low_cutoff) / 2, width, gain);
if(ntaps & 1)
{
phase = -freq * (ntaps >> 1);
}
else
{
phase = -freq / 2.0 * ((1 + 2 * ntaps) >> 1);
}
for(i = 0; i < ntaps; i++, phase += freq)
{
taps[i * 2 + 0] = lptaps[i] * cos(phase);
taps[i * 2 + 1] = lptaps[i] * sin(phase);
}
}
/* int16_t */
int fir_int16_init(fir_int16_t *s, const double *taps, unsigned int ntaps, int interpolation, int decimation, int delay)
{
int i, j;
s->type = 1;
s->interpolation = interpolation;
s->decimation = decimation;
/* Round number of taps up to a multiple of the interpolation factor */
s->ntaps = ntaps + (ntaps % interpolation ? interpolation - (ntaps % interpolation) : 0);
s->ataps = s->ntaps / interpolation;
s->itaps = calloc(s->ntaps, sizeof(int16_t));
s->qtaps = NULL;
/* Copy taps into the order they will be applied */
j = s->ntaps - s->ataps;
for(i = ntaps - 1; i >= 0; i--)
{
s->itaps[j] = lround(taps[i] * 32767.0);
j -= s->ataps;
if(j < 0) j += s->ntaps + 1;
}
s->lwin = s->ataps + delay;
s->win = calloc(s->ataps * 2 + delay, sizeof(int16_t));
s->owin = 0;
s->d = 0;
return(0);
}
size_t fir_int16_process(fir_int16_t *s, int16_t *out, const int16_t *in, size_t samples)
{
int a;
int x, y;
const int16_t *win, *taps;
if(s->type == 0) return(0);
else if(s->type == 2) return(fir_int16_complex_process(s, out, in, samples));
else if(s->type == 3) return(fir_int16_scomplex_process(s, out, in, samples));
for(x = 0; samples; samples--)
{
/* Append the next input sample to the round buffer */
s->win[s->owin] = *in;
if(s->owin < s->ataps) s->win[s->owin + s->lwin] = *in;
if(++s->owin == s->lwin) s->owin = 0;
for(; s->d < s->interpolation; s->d += s->decimation)
{
win = &s->win[s->owin];
taps = &s->itaps[s->d * s->ataps];
/* Calculate the next output sample */
for(a = y = 0; y < s->ataps; y++)
{
a += *(win++) * *(taps++);
}
a >>= 15;
*out = a < INT16_MIN ? INT16_MIN : (a > INT16_MAX ? INT16_MAX : a);
out += 2;
x++;
}
s->d -= s->interpolation;
in += 2;
}
return(x);
}
size_t fir_int16_process_block(fir_int16_t *s, int16_t *out, const int16_t *in, size_t samples)
{
int x;
/* Pre-fill buffer */
memset(s->win, 0, (s->lwin + s->ataps) * sizeof(int16_t));
s->owin = 0;
for(s->owin = 0; s->owin < s->ataps / 2; s->owin++, in += 2)
{
s->win[s->owin] = *in;
if(s->owin < s->ataps) s->win[s->owin + s->lwin] = *in;
}
x = fir_int16_process(s, out, in, samples);
return(x);
}
void fir_int16_free(fir_int16_t *s)
{
free(s->win);
free(s->itaps);
free(s->qtaps);
memset(s, 0, sizeof(fir_int16_t));
}
/* Initialise int16 FIR filter rational resampler */
int fir_int16_resampler_init(fir_int16_t *s, int interpolation, int decimation)
{
int ntaps;
double *taps;
int d;
/* Simplify ratio */
d = gcd(interpolation, decimation);
interpolation /= d;
decimation /= d;
/* Generate the filter taps */
ntaps = 21 * interpolation;
ntaps += (ntaps % interpolation ? interpolation - (ntaps % interpolation) : 0);
if((ntaps & 1) == 0) ntaps--;
taps = calloc(ntaps, sizeof(double));
if(!taps)
{
return(-1);
}
if(interpolation > decimation)
{
fir_low_pass(taps, ntaps, interpolation, 0.45, 0.1, interpolation);
}
else
{
fir_low_pass(taps, ntaps, interpolation, 0.45 * interpolation / decimation, 0.1 * interpolation / decimation, interpolation);
}
/* Create the FIR filter */
d = fir_int16_init(s, taps, ntaps, interpolation, decimation, 0);
free(taps);
return(d);
}
/* complex int16_t */
void fir_int16_complex_band_pass(int16_t *taps, size_t ntaps, double sample_rate, double low_cutoff, double high_cutoff, double width, double gain)
{
double *dtaps;
int i;
dtaps = calloc(ntaps, sizeof(double) * 2);
fir_complex_band_pass(dtaps, ntaps, sample_rate, low_cutoff, high_cutoff, width, gain);
for(i = 0; i < ntaps * 2; i++)
{
taps[i] = lround(dtaps[i] * 32767.0);
}
free(dtaps);
}
int fir_int16_complex_init(fir_int16_t *s, const double *taps, unsigned int ntaps, int interpolation, int decimation, int delay)
{
int i, j;
s->type = 2;
s->interpolation = interpolation;
s->decimation = decimation;
/* Round number of taps up to a multiple of the interpolation factor */
s->ntaps = ntaps + (ntaps % interpolation ? interpolation - (ntaps % interpolation) : 0);
s->ataps = s->ntaps / interpolation;
s->itaps = calloc(s->ntaps, sizeof(int16_t) * 2);
s->qtaps = calloc(s->ntaps, sizeof(int16_t) * 2);
/* Copy the taps in the order and format they are to be used */
j = s->ntaps - s->ataps;
for(i = ntaps - 1; i >= 0; i--)
{
s->itaps[j * 2 + 0] = lround( taps[i * 2 + 0] * 32767.0);
s->itaps[j * 2 + 1] = lround(-taps[i * 2 + 1] * 32767.0);
s->qtaps[j * 2 + 0] = lround( taps[i * 2 + 1] * 32767.0);
s->qtaps[j * 2 + 1] = lround( taps[i * 2 + 0] * 32767.0);
j -= s->ataps;
if(j < 0) j += s->ntaps + 1;
}
s->lwin = s->ataps + delay;
s->win = calloc(s->ataps * 2 + delay, sizeof(int16_t) * 2);
s->owin = 0;
s->d = 0;
return(0);
}
size_t fir_int16_complex_process(fir_int16_t *s, int16_t *out, const int16_t *in, size_t samples)
{
int32_t ai, aq;
int x, y;
const int16_t *win, *itaps, *qtaps;
for(x = 0; samples; samples--)
{
/* Append the next input sample to the round buffer */
s->win[s->owin * 2 + 0] = in[0];
s->win[s->owin * 2 + 1] = in[1];
if(s->owin < s->ataps)
{
s->win[(s->owin + s->lwin) * 2 + 0] = in[0];
s->win[(s->owin + s->lwin) * 2 + 1] = in[1];
}
if(++s->owin == s->lwin) s->owin = 0;
for(; s->d < s->interpolation; s->d += s->decimation)
{
win = &s->win[s->owin * 2];
itaps = &s->itaps[s->d * s->ataps];
qtaps = &s->qtaps[s->d * s->ataps];
/* Calculate the next output sample */
for(ai = aq = y = 0; y < s->ataps; y++)
{
ai += *(win++) * *(itaps++);
aq += *(win++) * *(qtaps++);
}
ai >>= 15;
aq >>= 15;
out[0] = ai < INT16_MIN ? INT16_MIN : (ai > INT16_MAX ? INT16_MAX : ai);
out[1] = aq < INT16_MIN ? INT16_MIN : (aq > INT16_MAX ? INT16_MAX : aq);
out += 2;
x++;
}
s->d -= s->interpolation;
in += 2;
}
return(x);
}
int fir_int16_scomplex_init(fir_int16_t *s, const double *taps, unsigned int ntaps, int interpolation, int decimation, int delay)
{
int i, j;
s->type = 3;
s->interpolation = interpolation;
s->decimation = decimation;
/* Round number of taps up to a multiple of the interpolation factor */
s->ntaps = ntaps + (ntaps % interpolation ? interpolation - (ntaps % interpolation) : 0);
s->ataps = s->ntaps / interpolation;
s->itaps = calloc(s->ntaps, sizeof(int16_t));
s->qtaps = calloc(s->ntaps, sizeof(int16_t));
/* Copy the taps in the order and format they are to be used */
j = s->ntaps - s->ataps;
for(i = ntaps - 1; i >= 0; i--)
{
s->itaps[j] = lround(taps[i * 2 + 0] * 32767.0);
s->qtaps[j] = lround(taps[i * 2 + 1] * 32767.0);
j -= s->ataps;
if(j < 0) j += s->ntaps + 1;
}
s->lwin = s->ataps + delay;
s->win = calloc(s->ataps * 2 + delay, sizeof(int16_t));
s->owin = 0;
s->d = 0;
return(0);
}
size_t fir_int16_scomplex_process(fir_int16_t *s, int16_t *out, const int16_t *in, size_t samples)
{
int32_t ai, aq;
int x, y;
const int16_t *win, *itaps, *qtaps;
for(x = 0; samples; samples--)
{
/* Append the next input sample to the round buffer */
s->win[s->owin] = *in;
if(s->owin < s->ataps) s->win[s->owin + s->lwin] = *in;
if(++s->owin == s->lwin) s->owin = 0;
for(; s->d < s->interpolation; s->d += s->decimation)
{
win = &s->win[s->owin];
itaps = &s->itaps[s->d * s->ataps];
qtaps = &s->qtaps[s->d * s->ataps];
/* Calculate the next output sample */
for(ai = aq = y = 0; y < s->ataps; y++)
{
ai += *win * *(itaps++);
aq += *(win++) * *(qtaps++);
}
ai >>= 15;
aq >>= 15;
out[0] = ai < INT16_MIN ? INT16_MIN : (ai > INT16_MAX ? INT16_MAX : ai);
out[1] = aq < INT16_MIN ? INT16_MIN : (aq > INT16_MAX ? INT16_MAX : aq);
out += 2;
x++;
}
s->d -= s->interpolation;
in += 2;
}
return(x);
}
/* int32_t */
int fir_int32_init(fir_int32_t *s, const double *taps, unsigned int ntaps, int interpolation, int decimation, int delay)
{
int i, j;
s->type = 1;
s->interpolation = interpolation;
s->decimation = decimation;
/* Round number of taps up to a multiple of the interpolation factor */
s->ntaps = ntaps + (ntaps % interpolation ? interpolation - (ntaps % interpolation) : 0);
s->ataps = s->ntaps / interpolation;
s->itaps = malloc(s->ntaps * sizeof(int32_t));
s->qtaps = NULL;
j = s->ntaps - s->ataps;
for(i = ntaps - 1; i >= 0; i--)
{
s->itaps[j] = lround(taps[i] * 32767.0);
j -= s->ataps;
if(j < 0) j += s->ntaps + 1;
}
s->lwin = s->ataps + delay;
s->win = calloc(s->ataps * 2 + delay, sizeof(int32_t));
s->owin = 0;
s->d = 0;
return(0);
}
size_t fir_int32_process(fir_int32_t *s, int32_t *out, const int32_t *in, size_t samples)
{
int64_t a;
int x, y;
const int32_t *win, *taps;
if(s->type == 0) return(0);
//else if(s->type == 2) return(fir_int32_complex_process(s, out, in, samples));
//else if(s->type == 3) return(fir_int32_scomplex_process(s, out, in, samples));
for(x = 0; samples; samples--)
{
/* Append the next input sample to the round buffer */
s->win[s->owin] = *in;
if(s->owin < s->ataps) s->win[s->owin + s->lwin] = *in;
if(++s->owin == s->lwin) s->owin = 0;
for(; s->d < s->interpolation; s->d += s->decimation)
{
win = &s->win[s->owin];
taps = &s->itaps[s->d * s->ataps];
/* Calculate the next output sample */
for(a = y = 0; y < s->ataps; y++)
{
a += (int64_t) *(win++) * (int64_t) *(taps++);
}
a >>= 15;
*out = a < INT32_MIN ? INT32_MIN : (a > INT32_MAX ? INT32_MAX : a);
out += 2;
x++;
}
s->d -= s->interpolation;
in += 2;
}
return(x);
}
void fir_int32_free(fir_int32_t *s)
{
free(s->win);
free(s->itaps);
free(s->qtaps);
memset(s, 0, sizeof(fir_int32_t));
}
/* IIR filter */
int iir_int16_init(iir_int16_t *s, const double *a, const double *b)
{
s->a[0] = a[0];
s->a[1] = a[1];
s->b[0] = b[0];
s->b[1] = b[1];
s->ix = 0;
s->iy = 0;
return(0);
}
size_t iir_int16_process(iir_int16_t *s, int16_t *out, const int16_t *in, size_t samples, size_t step)
{
size_t i;
for(i = 0; i < samples; i++)
{
s->iy = (double) *in * s->b[0] + s->ix * s->b[1] - s->iy * s->a[1];
s->ix = (double) *in;
*out = lround(s->iy < INT16_MIN ? INT16_MIN : (s->iy > INT16_MAX ? INT16_MAX : s->iy));
in += step;
out += step;
}
return(samples);
}
void iir_int16_free(iir_int16_t *s)
{
memset(s, 0, sizeof(iir_int16_t));
}
/* Soft Limiter */
void limiter_free(limiter_t *s)
{
fir_int32_free(&s->vfir);
fir_int32_free(&s->ffir);
free(s->shape);
free(s->att);
free(s->fix);
free(s->var);
}
int limiter_init(limiter_t *s, int16_t level, int width, const double *vtaps, const double *ftaps, int ntaps)
{
int i;
memset(s, 0, sizeof(limiter_t));
if(ntaps > 0)
{
if(vtaps)
{
i = fir_int32_init(&s->vfir, vtaps, ntaps, 1, 1, 0);
if(i != 0)
{
limiter_free(s);
return(-1);
}
}
if(ftaps)
{
i = fir_int32_init(&s->ffir, ftaps, ntaps, 1, 1, 0);
if(i != 0)
{
limiter_free(s);
return(-1);
}
}
}
/* Generate the limiter response shape */
s->width = width | 1;
s->shape = malloc(sizeof(int16_t) * s->width);
if(!s->shape)
{
limiter_free(s);
return(-1);
}
for(i = 0; i < s->width; i++)
{
s->shape[i] = lround((1.0 - cos(2.0 * M_PI / (s->width + 1) * (i + 1))) * 0.5 * INT16_MAX);
}
/* Initial state */
s->level = level;
s->att = calloc(sizeof(int16_t), s->width);
s->fix = calloc(sizeof(int32_t), s->width);
s->var = calloc(sizeof(int32_t), s->width);
if(!s->att || !s->fix || !s->var)
{
limiter_free(s);
return(-1);
}
s->p = 0;
s->h = s->width / 2;
return(0);
}
void limiter_process(limiter_t *s, int16_t *out, const int16_t *vin, const int16_t *fin, int samples, int step)
{
int i, j;
int32_t a, b;
for(i = 0; i < samples; i++)
{
s->var[s->p] = *vin;
s->fix[s->p] = (fin ? *fin : 0);
s->att[s->p] = 0;
/* Apply input filters */
if(s->vfir.type) fir_int32_process(&s->vfir, &s->var[s->p], &s->var[s->p], 1);
if(s->ffir.type) fir_int32_process(&s->ffir, &s->fix[s->p], &s->fix[s->p], 1);
/* Hard limit the fixed input */
if(s->fix[s->p] < -s->level) s->fix[s->p] = -s->level;
else if(s->fix[s->p] > s->level) s->fix[s->p] = s->level;
/* The variable signal is the difference between vin and fin */
s->var[s->p] -= s->fix[s->p];
if(++s->p == s->width) s->p = 0;
if(++s->h == s->width) s->h = 0;
/* Soft limit the variable input */
a = abs(s->var[s->h] + s->fix[s->h]);
if(a > s->level)
{
a = INT16_MAX - (s->level + abs(s->var[s->h]) - a) * INT16_MAX / abs(s->var[s->h]);
for(j = 0; j < s->width; j++)
{
b = (a * s->shape[j]) >> 15;
if(b > s->att[s->p]) s->att[s->p] = b;
if(++s->p == s->width) s->p = 0;
}
}
a = s->fix[s->p];
a += ((int64_t) s->var[s->p] * (INT16_MAX - s->att[s->p])) >> 15;
/* Hard limit to catch rounding errors */
if(a < -s->level) a = -s->level;
else if(a > s->level) a = s->level;
*out = a;
vin += step;
out += step;
if(fin) fin += step;
}
}