<< Chapter < Page | Chapter >> Page > |
1 /* v:\ece420\54x\dspclib\lab4bmain.c */
2 /* PN generation, IIR filtering, and autocorrelation added */
3 /* by Matt Kleffner - 9/2004 */
4 /* Original by dgs - 9/14/2001 */
5 /* Use governed by the Creative Commons Attribution License */
6
7 #include "v:\ece420\54x\dspclib\core.h"
8
9 /* #define N 1024 */ /* Number of FFT points */
10 #include "lab4b.h" /* Define N here in header file */
11
12 /* function defined in pn.c */
13 void rand_fillbuffer(void);
14 unsigned int *iseed; /* seed for rand_fillbuffer() and randbit() */
15
16 /* IIR values and buffers (declared in c_fft_given_iirc.asm) */
17 #define IIR_order 4
18 extern int scale;
19 extern int coef[IIR_order];
20 extern int state[IIR_order];
21
22 /* Pointer to state buffer location */
23 int iirptr;
24
25 /* function defined in iirfilt.c */
26 void iirfilt(void);
27
28 /* function defined in autocorr.c */
29 void autocorr(void);
30
31 /* Function defined by c_fft_given_iirc.asm */
32 void bit_rev_fft(void);
33
34 /* FFT data buffers (declared in c_fft_given_iirc.asm) */
35 extern int bit_rev_data[N*2]; /* Data input for bit-reverse function */
36 extern int fft_data[N*2]; /* In-place FFT & Output array */
37
38 /* Our input/output buffers */
39 int inputs[N];
40 int outputs[N];
41 int display_inputs[N];
42 int autocorr_in[N];
43 int autocorr_out[N];
44
45 volatile int input_full = 0; /* volatile means interrupt changes it */
46 int count = 0;
47
48 interrupt void irq(void)
49 {
50 int *Xmitptr,*Rcvptr; /* pointers to Xmit & Rcv Bufs */
51 int i;
52
53 static int in_irq = 0; /* Flag to prevent reentrance */
54
55 /* Make sure we're not in the interrupt (should never happen) */
56 if( in_irq )
57 return;
58
59 /* Mark we're processing, and enable interrupts */
60 in_irq = 1;
61 enable_irq();
62
63 /* The following waitaudio call is guaranteed not to
64 actually wait; it will simply return the pointers. */
65 WaitAudio(&Rcvptr,&Xmitptr);
66
67 /* input_full should never be true... */
68 if( !input_full )
69 {
70 for (i=0; i<BlockLen; i++)
71 {
72 /* Save input, send display_inputs to channel 1 */
73 inputs[count] = Rcvptr[4*i];
74 Xmitptr[6*i] = display_inputs[count];
75 /* inputs[count] = Xmitptr[6*i] = Rcvptr[4*i]; */
76
77 /* Send FFT output to channel 2 */
78 Xmitptr[6*i+1] = outputs[count];
79
80 count++;
81 }
82 /* Have we collected enough data yet? */
83 }
84 if( count >= N ) input_full = 1;
85
86 /* We're not in the interrupt anymore... */
87 disable_irq();
88 in_irq = 0;
89 }
90
91 main()
92 {
93 int i;
94 /* Initialize IRQ stuff */
95 count = 0;
96 input_full = 0;
97
98 /* Initialize autocorr_out to zero since some values will remain zero */
99 for (i = 0; i < N; ++i)
100 {
101 autocorr_out[i] = 0;
102 display_inputs[i] = 0;
103 }
104
105 /* Initialize PN-sequence generator */
106 *iseed = 1;
107
108 /* Initialize IIR filter states to zero */
109 iirptr = 0;
110 for (i = 0; i < IIR_order; ++i) state[i] = 0;
111
112 SetAudioInterrupt(irq); /* Set up interrupts */
113
114 while (1)
115 {
116 while( !input_full ); /* Wait for a data buffer to collect */
117
118 /* From here until we clear input_full can only take *
119 * BlockLen sample times, so don't do too much here. */
120
121 /* First, transfer inputs and outputs */
122
123 for (i = 0; i < N; i++) {
124 display_inputs[i] = autocorr_in[i];
125 outputs[i] = fft_data[i*2] << 8;
126
127 /* Some statements useful in debugging */
128 /* display_inputs[i] = inputs[i]; */
129 /* Be sure to comment out PN-sequence generation */
130 /* when using the next line */
131 /* autocorr_in[i] = 32767; inputs[i];*/
132 }
133 /* Last, set the DC coefficient to -1 for a trigger pulse */
134 outputs[0] = -32768;
135
136 /* Done with that... ready for new data collection */
137 count = 0; /* Need to reset the count */
138 input_full = 0; /* Mark we're ready to collect more data */
139
140 /*************************************************************/
141 /* Now that we've gotten the data moved, we can do the */
142 /* more lengthy processing. */
143
144 /* Generate PN input */
145 rand_fillbuffer();
146
147 /* Filter input */
148 iirfilt();
149
150 /* Calculate autocorrelation */
151 autocorr();
152
153 /* Transfer autocorr output to FFT input buffer */
154 for (i = 0; i < N; i++) {
155 bit_rev_data[i*2] = autocorr_out[i];
156 bit_rev_data[i*2+1] = 0;
157 }
158 /* Bit-reverse and compute FFT */
159 bit_rev_fft();
160
161 /* Done, wait for next time around! */
162 }
163 }
Notification Switch
Would you like to follow the 'Digital signal processing laboratory (ece 420)' conversation and receive update notifications?