Marcin Sierżan
Newbie level 3
Hi, i try to do a THD calculator using tms320c6713 in code composer studio. input - sin generator, output - scope. I've problem with otput signalamplitude. Can somebody look at the code? Images under post.
Code:
Code:
Code C - [expand] 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 #include <math.h> #include <stdlib.h> #include "dsk6713_aic23.h" Uint32 fs=DSK6713_AIC23_FREQ_48KHZ; #define PI 3.14159265 #define NFFT 1024 double THD(const double *fft); int findmax(const double *fft, const int ind); void doFFT(float *fin, double *fout); double thd_val; void main() { int input; double *mag; float *sig; float w[NFFT]; int i,k; mag = malloc((NFFT/2+1)* sizeof(double)); sig = malloc(NFFT* sizeof(float)); for(i = 0; i < NFFT; i++) { sig[i] = (float) 0.0; if(i<NFFT/2 +1) { mag[i]= (double) 0.0; gen_twiddle(w,NFFT); bit_rev(w,NFFT >> 1); comm_poll(); while(1) { input = input_sample(); sig[k] = (float) input/32768; if(k==0) { doFFT(sig,mag); thd_val = THD(mag); } k = (k+1)%NFFT; output_sample(input); } } void DSPF_sp_cfftr2_dit(float* x, float* w, short n) { short n2, ie, ia, i, j, k, m; float rtemp, itemp, c, s; n2 = n; ie = 1; for(k=n; k > 1; k >>= 1) { n2 >>= 1; ia = 0; for(j=0; j < ie; j++) { c = w[2*j]; s = w[2*j+1]; for(i=0; i < n2; i++) { m = ia + n2; rtemp = c * x[2*m] + s * x[2*m+1]; itemp = c * x[2*m+1] - s * x[2*m]; x[2*m] = x[2*ia] - rtemp; x[2*m+1] = x[2*ia+1] - itemp; x[2*ia] = x[2*ia] + rtemp; x[2*ia+1] = x[2*ia+1] + itemp; ia++; } ia += n2; } ie <<= 1; } } double THD(const double *fft) { int max,i,k,maxi; double sum; max = findmax(fft,1); k = (int) (NFFT/2 +1)/max; for(i=2;i < k;i++) { maxi = findmax(fft,max); sum += fft[maxi]*fft[maxi]; } return(sqrt(sum/(fft[max]*fft[max]))); } int findmax(const double *fft, const int ind) { int i,indmax; double tmpmax; static int counter; if(ind == 1) { tmpmax = fft[0]; indmax = 0; for(i = 1; i < (NFFT / 2 + 1); i++ ) { if(tmpmax < fft[i]) { tmpmax = fft[i]; indmax = i; } } counter=2; return(indmax); } else { tmpmax = 0; indmax = 0; for(i = counter*ind - 5 ; i < counter*ind+5; i++ ) { if(tmpmax < fft[i]) { tmpmax = fft[i]; indmax = i; } } counter++; return(indmax); } } void doFFT(float *fin, double *fout) { float *w; int i; double tmp; DSPF_sp_cfftr2_dit(fin,w,NFFT/2); bit_rev(fin,NFFT/2); for( i = 0 ; i < NFFT/2 ; i++ ) { tmp = (double) fin[2*i]*fin[2*i] + fin[2*i+1]*fin[2*i+1]; fout[i] = (double) sqrt(tmp); } } int gen_twiddle(float *w, int n) { double delta = 2 * PI / n; int i; for(i = 0; i < n/2; i++) { w[2 * i + 1] = sin(i * delta); w[2 * i] = cos(i * delta); } return n; } void bit_rev(float *x, int n) { int i, j, k; float rtemp, itemp; j = 0; for(i=1; i < (n-1); i++) { k = n >> 1; while(k <= j) { j -= k; k >>= 1; } j += k; if(i < j) { rtemp = x[j*2]; x[j*2] = x[i*2]; x[i*2] = rtemp; itemp = x[j*2+1]; x[j*2+1] = x[i*2+1]; x[i*2+1] = itemp; } } }
Last edited by a moderator: