-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhippo_fft.s
1401 lines (1172 loc) · 40.6 KB
/
hippo_fft.s
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;APS00000000000000000000000000000000000000000000000000000000000000000000000000000000
;; /* fix_fft.c - Fixed-point Fast Fourier Transform */
;; /*
;; fix_fft() perform FFT or inverse FFT
;; window() applies a Hanning window to the (time) input
;; fix_loud() calculates the loudness of the signal, for
;; each freq point. Result is an integer array,
;; units are dB (values will be negative).
;; iscale() scale an integer value by (numer/denom).
;; fix_mpy() perform fixed-point multiplication.
;; Sinewave[1024] sinewave normalized to 32767 (= 1.0).
;; Loudampl[100] Amplitudes for lopudnesses from 0 to -99 dB.
;; Low_pass Low-pass filter, cutoff at sample_freq / 4.
;; All data are fixed-point short integers, in which
;; -32768 to +32768 represent -1.0 to +1.0. Integer arithmetic
;; is used for speed, instead of the more natural floating-point.
;; For the forward FFT (time -> freq), fixed scaling is
;; performed to prevent arithmetic overflow, and to map a 0dB
;; sine/cosine wave (i.e. amplitude = 32767) to two -6dB freq
;; coefficients; the one in the lower half is reported as 0dB
;; by fix_loud(). The return value is always 0.
;; For the inverse FFT (freq -> time), fixed scaling cannot be
;; done, as two 0dB coefficients would sum to a peak amplitude of
;; 64K, overflowing the 32k range of the fixed-point integers.
;; Thus, the fix_fft() routine performs variable scaling, and
;; returns a value which is the number of bits LEFT by which
;; the output must be shifted to get the actual amplitude
;; (i.e. if fix_fft() returns 3, each value of fr[] and fi[]
;; must be multiplied by 8 (2**3) for proper scaling.
;; Clearly, this cannot be done within the fixed-point short
;; integers. In practice, if the result is to be used as a
;; filter, the scale_shift can usually be ignored, as the
;; result will be approximately correctly normalized as is.
;; TURBO C, any memory model; uses inline assembly for speed
;; and for carefully-scaled arithmetic.
;; Written by: Tom Roberts 11/8/89
;; Made portable: Malcolm Slaney 12/15/94 [email protected]
;; Timing on a Macintosh PowerBook 180.... (using Symantec C6.0)
;; fix_fft (1024 points) 8 ticks
;; fft (1024 points - Using SANE) 112 Ticks
;; fft (1024 points - Using FPU) 11
;; */
;; /* FIX_MPY() - fixed-point multiplication macro.
;; This macro is a statement, not an expression (uses asm).
;; BEWARE: make sure _DX is not clobbered by evaluating (A) or DEST.
;; args are all of type fixed.
;; Scaling ensures that 32767*32767 = 32767. */
;; #define dosFIX_MPY(DEST,A,B) { \
;; _DX = (B); \
;; _AX = (A); \
;; asm imul dx; \
;; asm add ax,ax; \
;; asm adc dx,dx; \
;; DEST = _DX; }
;; #define FIX_MPY(DEST,A,B) DEST = ((long)(A) * (long)(B))>>15
;; #define N_WAVE 1024 /* dimension of Sinewave[] */
N_WAVE = 1024
;; #define LOG2_N_WAVE 10 /* log2(N_WAVE) */
LOG2_N_WAVE = 10
;; #define N_LOUD 100 /* dimension of Loudampl[] */
N_LOUD = 100
;; #ifndef fixed
;; #define fixed short
;; #endif
;; extern fixed Sinewave[N_WAVE]; /* placed at end of this file for clarity */
;; extern fixed Loudampl[N_LOUD];
;; int db_from_ampl(fixed re, fixed im);
;; fixed fix_mpy(fixed a, fixed b);
;; /*
;; fix_fft() - perform fast Fourier transform.
;; if n>0 FFT is done, if n<0 inverse FFT is done
;; fr[n],fi[n] are real,imaginary arrays, INPUT AND RESULT.
;; size of data = 2**m
;; set inverse to 0=dft, 1=idft
;; */
FFT_TEST = 0
* Enables different way to handle fixed points,
* faster but breaks the test. Though it should still be
* accurate.
FFT_ALTERNATE_ASR = 1
ifne FFT_TEST
FFT_SIZE = 4
else
FFT_SIZE = 7
endif
FFT_LENGTH = 1<<FFT_SIZE
FFT_LOOPS = 0
; disable multitasking for more accuracy
ifne FFT_TEST
incdir "include:"
include "exec/exec_lib.i"
test
move.l 4.w,a6
jsr _LVOForbid(a6)
jsr _LVODisable(a6)
bsr convert_sine
bsr.w .waitVBlank
bsr testFFT
bsr testFFT
bsr testFFT
move.l $dff004,d6
and.l #$1ff00,d6
lsr.l #8,d6
* FS-UAE A500 kick13+68000:
* - 165
* - 163: sine conversion ASRs removed
* - 162: use SP for vars, a5 for another sine pointer
* - 160: use all table indexes multiplied by two
* - 139: do asr #15 to the sum instead of components before
* this breaks the unit test but probably
* because it is just more accurate.
* - 125: replace asr #15 with add,swap
* - 123: use the now free d5 as .m(sp)
move.l 4.w,a6
jsr _LVOEnable(a6)
jsr _LVOPermit(a6)
rts
.waitVBlank
.v1 btst #0,$dff005
beq.b .v1
.v2 btst #0,$dff005
bne.b .v2
rts
testFFT
lea .test_real_in(pc),a0
lea fr(pc),a1
moveq #FFT_LENGTH-1,d0
.c move (a0)+,(a1)+
dbf d0,.c
lea fr(pc),a0
;bsr windowFFT
lea fr(pc),a0
lea fi(pc),a1
bsr.w sampleFFT
moveq #-1,d7
moveq #FFT_LENGTH-1,d0
lea .test_real_out(pc),a0
lea fr(pc),a1
.ch1 cmpm.w (a0)+,(a1)+
bne.b .error
dbf d0,.ch1
moveq #FFT_LENGTH-1,d0
lea .test_img_out(pc),a0
lea fi(pc),a1
.ch2 cmpm.w (a0)+,(a1)+
bne.b .error
dbf d0,.ch2
;; passed
moveq #0,d7
; lea fr(pc),a0
; lea fi(pc),a1
; bsr loudFFT
; rts
; lea fr(pc),a0
; lea fi(pc),a1
; move.l d7,-(sp)
; bsr.w calcFFTPower
; move.l (sp)+,d7
.error
movem.l cloop1,d0-d4
rts
.test_real_in
dc.w 1000,923,707,382,0,-382,-707,-923,-1000,-923,-707,-382,0,382,707,923
.test_real_out
dc.w -2,498,0,0,0,1,1,0,0,0,0,0,0,-1,-1,498
.test_img_out
dc.w 0,-3,-1,-2,0,-1,-1,-2,0,1,1,0,0,1,1,2
temp dc.l 0
cloop1 dc.l 0
cloop2 dc.l 0
cloop3 dc.l 0
cloop4 dc.l 0
cloop5 dc.l 0
maxSqr dc.l 0
temp2 dc.l 0
endif ; FFT_TEST
if 0
prepareSquareTable
lea squareTable,a0
move #$ffff,d1
moveq #0,d0
.l
move d0,d2
muls d2,d2
move.l d2,(a0)+
addq #1,d0
cmp d0,d1
bne.b .l
rts
endif
;in
; a0 = result array reals
; a1 = result array imaginary
;out
; a0 = result (overwritten input array)
; REM ;;;;;;;;;;;;;,
;calcFFTPower
; ; Calculate for the 1st half, 2nd half is mirror of the 1st and
; ; not used in drawing.
; moveq #FFT_LENGTH/2-1,d7
; move.l #$ffff,d6
;; lea squareTable,a2
;.l
; move (a0),d0
; move (a1)+,d1
; muls d0,d0
; muls d1,d1
; add.l d1,d0
;
;
;; cmp.l maxSqr(pc),d0
;; blo.b .s
;; move.l d0,maxSqr
;; move (a0),temp
;; move -2(a1),temp2
;;.s
; ; See which square root to use
; cmp.l d6,d0
; bls.b .16
;
; bsr.b isqrt32
;
; move d1,(a0)+
; dbf d7,.l
; rts
;.16
; bsr.b isqrt16
; move d1,(a0)+
; dbf d7,.l
; rts
;
;
; incdir
; include "isqrt16.s"
; EREM ;;;;;;;;;;;;;;;;;
ifne FFT_TEST
fi ds.w FFT_LENGTH
fr ds.w FFT_LENGTH
fpow ds.w FFT_LENGTH
endif
ifne FFT_TEST
convert_sine
lea Sinewave(pc),a3
move #N_WAVE-1,d0
.loop
asr (a3)+
dbf d0,.loop
rts
endif
; in
; a0 = 16-bit signed sampledata (real array)
; a1 = imaginary array, to be overwritten
; a2 = sinewave
; out
; a0 = result array, real (overwritten input)
; a1 = result array, imaginary
sampleFFT
; clear first half of the imaginary array
move.l a1,a3
moveq #FFT_LENGTH/2/4-1,d0
moveq #0,d6
.c
move.l d6,(a3)+
move.l d6,(a3)+
move.l d6,(a3)+
move.l d6,(a3)+
dbf d0,.c
fix_fft
rsreset
.m rs.w 1
.k rs.w 1
.i rs.w 1
.l rs.w 1
.istep rs.w 1
.varsSizeof rs.b 0
movem.l a4/a5/a6,-(sp)
lea -.varsSizeof(sp),sp
;lea .vars(pc),a5
; m, data size 1<<7 = 128
move #FFT_SIZE,.m(sp)
ifne FFT_TEST
lea Sinewave(pc),a2
endif
; Cosine
lea N_WAVE/4*2(a2),a5
;; int fix_fft(fixed fr[], fixed fi[], int m, int inverse)
;; {
;; int mr,nn,i,j,l,k,istep, n, scale, shift;
;; fixed qr,qi,tr,ti,wr,wi,t;
;; n = 1<<m;
.n = FFT_LENGTH
;; if(n > N_WAVE)
;; return -1;
;; mr = 0;
; clr.w .mr(sp)
;; nn = n - 1;
.nn = .n-1
;; /* decimation in time - re-order data */
;; for(m=1; m<=nn; ++m) {
moveq #.nn-1,d7 ; loop counter
moveq #.nn,d3 ; loop comparison
; Cleared earlier:
; moveq #0,d6 ; mr
moveq #1,d5 ; m
move #.n,d4 ; preloaded constant
lea (.n*2).w,a6 ; loop condition constant for loop5 preloaded
;; ------------------------------------------------------------------
; top level loop
.loop1
; loop 1 run 127 times when FFT_LENGTH = 128
ifne FFT_LOOPS
addq.l #1,cloop1
endif
;; l = n;
move d4,d0 ; d4 = .n
;; ------------------------------------------------------------------
.loop2
; loop 2 run 247 times when FFT_LENGTH = 128
ifne FFT_LOOPS
addq.l #1,cloop2
endif
;; do {
;; l >>= 1;
lsr.w #1,d0
;; } while(mr+l > nn);
move.w d6,d2
add.w d0,d2
cmp.w d3,d2
bhi.b .loop2
;; ------------------------------------------------------------------
;move.w d0,.l(sp)
;; mr = (mr & (l-1)) + l;
; d6 = mr
move.w d0,d1
subq.w #1,d1 ; l-1
and.w d6,d1 ; d6 = mr
add.w d0,d1
move.w d1,d6
;; if(mr <= m) continue;
cmp.w d5,d1 ; d5 = m
bls.b .continue
add d1,d1 ; mr index
move.w d5,d2 ; m index
add d2,d2
;; tr = fr[m];
; d0 = tr/ti
move (a0,d2.w),d0
;; fr[m] = fr[mr];
move (a0,d1.w),(a0,d2.w)
;; fr[mr] = tr;
move d0,(a0,d1.w)
;; ti = fi[m];
move (a1,d2.w),d0
;; fi[m] = fi[mr];
move (a1,d1.w),(a1,d2.w)
;; fi[mr] = ti;
move d0,(a1,d1.w)
;; ------------------------------------------------------------------
; loop condition and increment
.continue
addq #1,d5 ; d5 = m
dbf d7,.loop1
;; }
;; l = 1;
move.w #1*2,.l(sp) * index
;; k = LOG2_N_WAVE-1;
move.w #LOG2_N_WAVE-1,.k(sp)
ifeq FFT_ALTERNATE_ASR
moveq #15,d5 ; shift for multiplications for loop 5
endif
;; ------------------------------------------------------------------
; top level loop
.loop3
; loop 3 run 7 times when FFT_LENGTH = 128
ifne FFT_LOOPS
addq.l #1,cloop3
endif
;; while(l < n) {
;; /* it may not be obvious, but the shift will be performed
;; on each data point exactly once, during this pass. */
;; istep = l << 1;
move.w .l(sp),d0 * index
add.w d0,d0
move.w d0,.istep(sp) * index
;; for(m=0; m<l; ++m) {
; clr.w .m(sp)
ifne FFT_ALTERNATE_ASR
moveq #0,d5
else
clr.w (sp)
endif
;; ------------------------------------------------------------------
.loop4
ifne FFT_LOOPS
addq.l #1,cloop4
endif
; loop 4 run 127 times when FFT_LENGTH = 128
;; j = m << k;
; move.w .m(sp),d0
; move.w (sp),d0
; move.w .k(sp),d1
ifne FFT_ALTERNATE_ASR
move d5,d0
move d5,d6
move .k(sp),d1
lsl d1,d0
else
movem.w (sp),d0/d1 ; load both .m an .k, stored sequentially
move d0,d6 * index .m
lsl d1,d0
endif
;; wi = -Sinewave[j];
;; wi >>= 1;
;add.w d0,d0
move.w (a2,d0.w),d1
neg.w d1
; asr.w #1,d1
move d1,a4
; a4 = wi
;; wr = Sinewave[j+N_WAVE/4];
;; wr >>= 1;
; add.w #N_WAVE/4*2,d0
; move.w (a2,d0.w),d1
; asr.w #1,d1
; move d1,a3
move (a5,d0.w),a3
; a3 = wr
;; ------------------------------------------------------------------
;; for(i=m; i<n; i+=istep) {
;move.w .m(sp),d6 ; i
;move.w (sp),d6 ; i - load a few lins above already
move .l(sp),d7 ; j
add d6,d7
; add d6,d6 ; i table index
; add d7,d7 ; j table index
; loop increment
; use double as d6 and d7 are word table indices
move .istep(sp),d0 * index
; add d0,d0
; loop condition is in a6
.loop5
ifne FFT_LOOPS
addq.l #1,cloop5
endif
;speed 162 to 160 when removed moveq #15,d5 from inside the loop
; loop 5 run 448 times when FFT_LENGTH = 128
;; j = i + l;
; step one at loop end
;; tr = fix_mpy(wr,fr[j])-fix_mpy(wi,fi[j]);
move (a0,d7),d3 ; fr(j)
move (a1,d7),d4 ; fi(j)
move.w a3,d1 ; fr(j)*wr
muls.w d3,d1
ifeq FFT_ALTERNATE_ASR
asr.l d5,d1
endif
move.w a4,d2 ; fi(j)*wi
muls.w d4,d2
ifeq FFT_ALTERNATE_ASR
asr.l d5,d2
endif
sub.l d2,d1
ifne FFT_ALTERNATE_ASR
; Tricky asr #15
; Calculations use 16-bit words with fixed point at bit 15.
; Multiplying these will yield a value where the fixed point is at 30,
; with one sign bit and one value bit. Doing an add.l d1,d1 could cause
; an overflow but in practice it seems to work fine. Much faster
; than asr.l #15.
add.l d1,d1
swap d1
endif
; d1 = tr
;; qr = fr[i];
;; qr >>= 1;
move.w (a0,d6.w),d2
asr.w #1,d2
; d2 = qr
;; Use of d5 or not:
if 0
;; fr[j] = qr - tr
move d2,d5
sub d1,d5
move d5,(a0,d7.w)
;; fr[i] = qr + tr
add.w d1,d2
move d2,(a0,d6.w)
else
;; fr[j] = qr - tr
sub d1,d2
move d2,(a0,d7.w)
;; fr[i] = qr + tr
add.w d1,d2
add.w d1,d2
move d2,(a0,d6.w)
endif
;; ti = fix_mpy(wr,fi[j])+fix_mpy(wi,fr[j]);
move.w a3,d1 ; fi(j)*wr
muls.w d4,d1
ifeq FFT_ALTERNATE_ASR
asr.l d5,d1
endif
move.w a4,d2 ; fr(j)*wi
muls.w d3,d2
ifeq FFT_ALTERNATE_ASR
asr.l d5,d2
endif
add.l d2,d1
ifne FFT_ALTERNATE_ASR
; Tricky asr #15
add.l d1,d1
swap d1
endif
; d1 = ti
;; qi = fi[i];
;; qi >>= 1;
move.w (a1,d6.w),d2
asr.w #1,d2
; d2 = qi
;; Use of d5 or not:
if 0
;; fi[j] = qi - ti
move d2,d5
sub d1,d5
move d5,(a1,d7.w)
;; fi[i] = qi + ti
add d1,d2
move d2,(a1,d6.w)
else
;; fi[j] = qi - ti
sub d1,d2
move d2,(a1,d7.w)
;; fi[i] = qi + ti
add d1,d2
add d1,d2
move d2,(a1,d6.w)
endif
;; }
; for loop: for(i=m; i<n; i+=istep)
; add istep twice as d6 and d7 are word table indices
; d0 = 2*istep
add d0,d6 ; i+=istep
add d0,d7 ; j+=istep
; a6 = 2*.n
cmp.w a6,d6
blo.b .loop5
;; }
ifne .m
fail .m referred without index, so it must be zero
endif
;for loop: for(m=0; m<l; ++m)
ifne FFT_ALTERNATE_ASR
addq #1*2,d5
cmp .l(sp),d5
blo.b .loop4
else
addq #1*2,(sp)
;move.w .m(sp),d0
move.w (sp),d0
cmp.w .l(sp),d0
blo.b .loop4
endif
;; --k;
subq.w #1,.k(sp)
;; l = istep;
move.w .istep(sp),.l(sp)
;; }
; loop condition: while(l < n)
; move.w .l(sp),d0
; cmp.w #.n,d0
cmp.w #.n*2,.l(sp)
blo.w .loop3
;; return scale;
;; }
; DONE
lea .varsSizeof(sp),sp
movem.l (sp)+,a4/a5/a6
rts
;; /* window() - apply a Hanning window */
;; void window(fixed fr[], int n)
; in:
; a0 = input data
; a2 = sinewave
windowFFT
;; {
;; int i,j,k;
;; j = N_WAVE/n;
; sinewave index step
move #N_WAVE/FFT_LENGTH*2,d7
;; n >>= 1;
; loop end condition
moveq #FFT_LENGTH/2-1,d6
;; for(i=0,k=N_WAVE/4; i<n; ++i,k+=j)
;; FIX_MPY(fr[i],fr[i],16384-(Sinewave[k]>>1));
;i=0
;k=N_WAVE/4
;index to sinewave
;lea Sinewave+N_WAVE/4*2(pc),a1
lea N_WAVE/4*2(a2),a1
moveq #15,d3 ; muls shift
move #16384,d2
.for1
; 16384-(Sinewave(k)>>1)
move (a1),d4
;asr #1,d4 * sinetable prescaled earlier
move d2,d5
sub d4,d5
; fr(i)
muls (a0),d5
asr.l d3,d5
move d5,(a0)+
; ++i
;addq #1,d0
; k+=j
;add d7,d1
add d7,a1
;i<n
dbf d6,.for1
;; n <<= 1;
; loop end condition
; add d6,d6
moveq #FFT_LENGTH/2-1,d6
;; for(k-=j; i<n; ++i,k-=j)
;; FIX_MPY(fr[i],fr[i],16384-(Sinewave[k]>>1));
;; }
; k-=j
;sub d7,d1
sub d7,a1
.for2
; 16384-(Sinewave(k)>>1)
move (a1),d4
;asr #1,d4 * sinetable prescaled earlier
move d2,d5
sub d4,d5
muls (a0),d5
asr.l d3,d5
move d5,(a0)+
;++i
;addq #1,d0
;k-=j
;sub d7,d1
sub d7,a1
;i<n
dbf d6,.for2
rts
;; /* fix_loud() - compute loudness of freq-spectrum components.
;; n should be ntot/2, where ntot was passed to fix_fft();
;; 6 dB is added to account for the omitted alias components.
;; scale_shift should be the result of fix_fft(), if the time-series
;; was obtained from an inverse FFT, 0 otherwise.
;; loud[] is the loudness, in dB wrt 32767; will be +10 to -N_LOUD.
;; */
;; void fix_loud(fixed loud[], fixed fr[], fixed fi[], int n, int scale_shift)
;; {
;; int i, max;
;; max = 0;
;; if(scale_shift > 0)
;; max = 10;
;; scale_shift = (scale_shift+1) * 6;
;; for(i=0; i<n; ++i) {
;; loud[i] = db_from_ampl(fr[i],fi[i]) + scale_shift;
;; if(loud[i] > max)
;; loud[i] = max;
;; }
;; }
;in
; a0 = result array reals
; a1 = result array imaginary
;out
; a0 = result (overwritten input array)
; rem ;;;;;;;;;
;loudFFT
; lea Loudampl(pc),a2
; lea Loudampl2(pc),a3
;
; tst.l (a3)
; bne.b .1
;
; move (a2)+,d0
; muls d0,d0
; move.l d0,(a3)+
;
; moveq #N_LOUD-1-1,d7
;.l1
; move (a2)+,d0
; muls d0,d0
; move.l d0,(a3)
; add.l -4(a3),d0
; lsr.l #1,d0
; move.l d0,-4(a3)
; addq.l #4,a3
; dbf d7,.l1
;.1
;
;nok
; moveq #FFT_LENGTH/2-1,d7
;.l2
; move (a0)+,d0
; muls d0,d0
; move (a1)+,d1
; muls d1,d1
; add.l d1,d0
;
; lea Loudampl2(pc),a2
; moveq #0,d6
; moveq #N_LOUD,d5
;.l3
; cmp.l (a2)+,d0
; bhi.b .break
;
; addq #1,d6
; ;cmp #N_LOUD,d6
; cmp d5,d6
; bne.b .l3
;
;.break
; * d6 = dB level
; ;neg d6
; ;addq #6,d6
; ;bmi.b .2
; ;moveq #0,d6
;.2
;
; subq #6,d6
; bpl.b .3
; moveq #0,d6
;.3
; move d6,-2(a0)
;
; dbf d7,.l2
;
;
; rts
; EREM ;;;;;;;;;;;;;;;
;; /* db_from_ampl() - find loudness (in dB) from
;; the complex amplitude.
;; */
;; int db_from_ampl(fixed re, fixed im)
;; {
;; static long loud2[N_LOUD] = {0};
;; long v;
;; int i;
;; if(loud2[0] == 0) {
;; loud2[0] = (long)Loudampl[0] * (long)Loudampl[0];
;; for(i=1; i<N_LOUD; ++i) {
;; v = (long)Loudampl[i] * (long)Loudampl[i];
;; loud2[i] = v;
;; loud2[i-1] = (loud2[i-1]+v) / 2;
;; }
;; }
;; v = (long)re * (long)re + (long)im * (long)im;
;; for(i=0; i<N_LOUD; ++i)
;; if(loud2[i] <= v)
;; break;
;; return (-i);
;; }
;; /*
;; fix_mpy() - fixed-point multiplication
;; */
;; fixed fix_mpy(fixed a, fixed b)
;; {
;; FIX_MPY(a,a,b);
;; return a;
;; }
;; /*
;; iscale() - scale an integer value by (numer/denom)
;; */
;; int iscale(int value, int numer, int denom)
;; {
;; #ifdef DOS
;; asm mov ax,value
;; asm imul WORD PTR numer
;; asm idiv WORD PTR denom
;; return _AX;
;; #else
;; return (long) value * (long)numer/(long)denom;
;; #endif
;; }
;; /*
;; fix_dot() - dot product of two fixed arrays
;; */
;; fixed fix_dot(fixed *hpa, fixed *pb, int n)
;; {
;; fixed *pa;
;; long sum;
;; register fixed a,b;
;; unsigned int seg,off;
;; /* seg = FP_SEG(hpa);
;; off = FP_OFF(hpa);
;; seg += off>>4;
;; off &= 0x000F;
;; pa = MK_FP(seg,off);
;; */
;; sum = 0L;
;; while(n--) {
;; a = *pa++;
;; b = *pb++;