Versão sem FFT e aquisição por DMA. 256 amostras.

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Committer:
rebonatto
Date:
Tue Jan 05 11:45:44 2016 +0000
Revision:
0:e57bc370d339
Vers?o est?vel sem calculo de FFT. Aquisi??o por DMA. Usa 256 amostras.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:e57bc370d339 1 /*
rebonatto 0:e57bc370d339 2 * SignalProcessor.cpp
rebonatto 0:e57bc370d339 3 *
rebonatto 0:e57bc370d339 4 * Created on:
rebonatto 0:e57bc370d339 5 * Author:
rebonatto 0:e57bc370d339 6 */
rebonatto 0:e57bc370d339 7
rebonatto 0:e57bc370d339 8 #include <math.h>
rebonatto 0:e57bc370d339 9 #include <stdlib.h>
rebonatto 0:e57bc370d339 10 #include <stdio.h>
rebonatto 0:e57bc370d339 11 #include <string.h>
rebonatto 0:e57bc370d339 12
rebonatto 0:e57bc370d339 13 #include "SignalProcessor.h"
rebonatto 0:e57bc370d339 14 #include "limites.h"
rebonatto 0:e57bc370d339 15
rebonatto 0:e57bc370d339 16 #define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
rebonatto 0:e57bc370d339 17 #define PI 3.14159265358979323846F
rebonatto 0:e57bc370d339 18 // 3.141597653564793332212487132
rebonatto 0:e57bc370d339 19 // 3.14159265358979323846
rebonatto 0:e57bc370d339 20
rebonatto 0:e57bc370d339 21
rebonatto 0:e57bc370d339 22 /* Elementos vm2, under, over adicionados em 20/05/2014 por Rebonatto */
rebonatto 0:e57bc370d339 23 /* vm2 eh o calculo do valor medio, under eh a cotagem dos valores do AD com 0 */
rebonatto 0:e57bc370d339 24 /* over e a contagem do AD com 4095 */
rebonatto 0:e57bc370d339 25 /* over e under sao para verificar se o processo de ajuste dos dados esta Ok e vm2 para conferir o vm da fft */
rebonatto 0:e57bc370d339 26 void SignalProcessor::CalculateRMSBulk(float *result, float *vm2, int *under, int *over)
rebonatto 0:e57bc370d339 27 {
rebonatto 0:e57bc370d339 28 int nChannel,nSample;
rebonatto 0:e57bc370d339 29
rebonatto 0:e57bc370d339 30 for(nChannel=0;nChannel<NUMBER_OF_CHANNELS;nChannel++)
rebonatto 0:e57bc370d339 31 result[nChannel] = vm2[nChannel] = under[nChannel] = over[nChannel] = 0;
rebonatto 0:e57bc370d339 32
rebonatto 0:e57bc370d339 33 for(nChannel=0;nChannel<NUMBER_OF_CHANNELS;nChannel++)
rebonatto 0:e57bc370d339 34 {
rebonatto 0:e57bc370d339 35 for(nSample=0;nSample<NUMBER_OF_SAMPLES;nSample++)
rebonatto 0:e57bc370d339 36 {
rebonatto 0:e57bc370d339 37 unsigned short int v = Capture::GetValue(nSample, nChannel);
rebonatto 0:e57bc370d339 38 /* novos calculos */
rebonatto 0:e57bc370d339 39 vm2[nChannel] += v;
rebonatto 0:e57bc370d339 40 if (v <= 20)
rebonatto 0:e57bc370d339 41 under[nChannel] = under[nChannel] + 1;
rebonatto 0:e57bc370d339 42 if (v >= 4075)
rebonatto 0:e57bc370d339 43 over[nChannel] = over[nChannel] + 1;
rebonatto 0:e57bc370d339 44 float val = (float)v;
rebonatto 0:e57bc370d339 45
rebonatto 0:e57bc370d339 46 val -= Settings::get_Offset(nChannel);
rebonatto 0:e57bc370d339 47 val /= Settings::get_Gain(nChannel);
rebonatto 0:e57bc370d339 48 val *= val;
rebonatto 0:e57bc370d339 49 result[nChannel] += val;
rebonatto 0:e57bc370d339 50 }
rebonatto 0:e57bc370d339 51 result[nChannel] /= (float)NUMBER_OF_SAMPLES;
rebonatto 0:e57bc370d339 52 result[nChannel] = sqrt(result[nChannel]);
rebonatto 0:e57bc370d339 53
rebonatto 0:e57bc370d339 54 /* novos calculos */
rebonatto 0:e57bc370d339 55 vm2[nChannel] /= (float)NUMBER_OF_SAMPLES;
rebonatto 0:e57bc370d339 56 }
rebonatto 0:e57bc370d339 57 }
rebonatto 0:e57bc370d339 58
rebonatto 0:e57bc370d339 59 float SignalProcessor::CalculateRMS(short int *buffer,int nChannel)
rebonatto 0:e57bc370d339 60 {
rebonatto 0:e57bc370d339 61 float result=0;
rebonatto 0:e57bc370d339 62 int nSample;
rebonatto 0:e57bc370d339 63
rebonatto 0:e57bc370d339 64 for(nSample=0;nSample<NUMBER_OF_SAMPLES;nSample++)
rebonatto 0:e57bc370d339 65 {
rebonatto 0:e57bc370d339 66 short int v = buffer[nSample];
rebonatto 0:e57bc370d339 67 float val = (float)v;
rebonatto 0:e57bc370d339 68 // cada ponto
rebonatto 0:e57bc370d339 69 //val -= Settings::get_Offset(nChannel); // diminui o offset
rebonatto 0:e57bc370d339 70 val /= Settings::get_Gain(nChannel); // divide pelo ganhp
rebonatto 0:e57bc370d339 71 val *= val; // eleva ao quadrado
rebonatto 0:e57bc370d339 72 result += val; // soma
rebonatto 0:e57bc370d339 73 }
rebonatto 0:e57bc370d339 74 result /= (float)NUMBER_OF_SAMPLES; // divide pelo numero de amostras (256)
rebonatto 0:e57bc370d339 75 result = sqrt(result);
rebonatto 0:e57bc370d339 76 return result;
rebonatto 0:e57bc370d339 77 }
rebonatto 0:e57bc370d339 78
rebonatto 0:e57bc370d339 79 float SignalProcessor::CalculateRMSFloat( float *buffer,int nChannel)
rebonatto 0:e57bc370d339 80 {
rebonatto 0:e57bc370d339 81 float result=0, val;
rebonatto 0:e57bc370d339 82 int nSample;
rebonatto 0:e57bc370d339 83
rebonatto 0:e57bc370d339 84 for(nSample=0;nSample<NUMBER_OF_SAMPLES;nSample++)
rebonatto 0:e57bc370d339 85 {
rebonatto 0:e57bc370d339 86 val = buffer[nSample];
rebonatto 0:e57bc370d339 87 /*
rebonatto 0:e57bc370d339 88 short int v = buffer[nSample];
rebonatto 0:e57bc370d339 89 float val = (float)v;
rebonatto 0:e57bc370d339 90 */ // cada ponto
rebonatto 0:e57bc370d339 91 //val -= Settings::get_Offset(nChannel); // diminui o offset
rebonatto 0:e57bc370d339 92 val /= Settings::get_Gain(nChannel); // divide pelo ganho
rebonatto 0:e57bc370d339 93 val *= val; // eleva ao quadrado
rebonatto 0:e57bc370d339 94 result += val; // soma
rebonatto 0:e57bc370d339 95 }
rebonatto 0:e57bc370d339 96 result /= NUMBER_OF_SAMPLES; // divide pelo numero de amostras (256)
rebonatto 0:e57bc370d339 97 result = sqrt(result);
rebonatto 0:e57bc370d339 98 return result;
rebonatto 0:e57bc370d339 99 }
rebonatto 0:e57bc370d339 100
rebonatto 0:e57bc370d339 101
rebonatto 0:e57bc370d339 102 void SignalProcessor::CalculateFFT(float *buffer,float *sen,float *cos,float *vm,int sign, int ch)
rebonatto 0:e57bc370d339 103 {
rebonatto 0:e57bc370d339 104 /*
rebonatto 0:e57bc370d339 105 for(int i=0; i < Settings::get_Samples(); i++)
rebonatto 0:e57bc370d339 106 printf("%d*",buffer[i]);
rebonatto 0:e57bc370d339 107 printf("\n");
rebonatto 0:e57bc370d339 108 */
rebonatto 0:e57bc370d339 109
rebonatto 0:e57bc370d339 110 //printf("[0] %d %d %d %d\n", buffer[0], buffer[100], buffer[200], buffer[255]);
rebonatto 0:e57bc370d339 111 /*
rebonatto 0:e57bc370d339 112 for(i=0; i<Settings::get_Samples();i++)
rebonatto 0:e57bc370d339 113 value[i]= (float) ( (buffer[i] - Settings::get_Offset(ch)) / Settings::get_Gain(ch) );
rebonatto 0:e57bc370d339 114 */
rebonatto 0:e57bc370d339 115
rebonatto 0:e57bc370d339 116 //float* fft = ComplexFFT(buffer,1, ch); //deve desalocar memoria do ptr retornado
rebonatto 0:e57bc370d339 117 ComplexFFT(buffer,1, ch); //deve desalocar memoria do ptr retornado
rebonatto 0:e57bc370d339 118
rebonatto 0:e57bc370d339 119 /* usado em teste
rebonatto 0:e57bc370d339 120 *vm = ComplexFFT(buffer, fft, 1, ch); //deve desalocar memoria do ptr retornado
rebonatto 0:e57bc370d339 121 */
rebonatto 0:e57bc370d339 122 /*
rebonatto 0:e57bc370d339 123 Mapa do vetor fft.
rebonatto 0:e57bc370d339 124 O vetor tem 2 vezes o no. de amostras. Cada par de valores (portanto n e n+1), representam, respectivamente
rebonatto 0:e57bc370d339 125 COS e SEN.
rebonatto 0:e57bc370d339 126 Os dois primeiros valores reprensetam a frequencia 0Hz, portanto sao atribuidas ao valor medio.
rebonatto 0:e57bc370d339 127 Os demais pares de valores representam a fundamental e suas harmonicas,
rebonatto 0:e57bc370d339 128 sendo que se a fundamental for 60Hz, teremos: 60,120,180,240...
rebonatto 0:e57bc370d339 129 Para a nossa aplicacao apenas as 12 primeiras harmonicas serao utilizadas (720Hz)
rebonatto 0:e57bc370d339 130 */
rebonatto 0:e57bc370d339 131
rebonatto 0:e57bc370d339 132 //*vm = DFT(value, sen, cos);
rebonatto 0:e57bc370d339 133
rebonatto 0:e57bc370d339 134 *vm = buffer[0];
rebonatto 0:e57bc370d339 135 //printf("Valor medio da FFT %.4f \n", buffer[0]);
rebonatto 0:e57bc370d339 136
rebonatto 0:e57bc370d339 137 for(int i=1;i<Settings::get_MaxHarmonics()+1;i++)
rebonatto 0:e57bc370d339 138 {
rebonatto 0:e57bc370d339 139 cos[i-1] = buffer[i*2];
rebonatto 0:e57bc370d339 140 sen[i-1] = buffer[i*2+1];
rebonatto 0:e57bc370d339 141 }
rebonatto 0:e57bc370d339 142
rebonatto 0:e57bc370d339 143 /*
rebonatto 0:e57bc370d339 144 for(int i=0;i<Settings::get_MaxHarmonics();i++)
rebonatto 0:e57bc370d339 145 {
rebonatto 0:e57bc370d339 146 printf("[%dHz]\tsen %.4f\tcos %.4f\n", (i+1)*60, sen[i], cos[i]);
rebonatto 0:e57bc370d339 147 }
rebonatto 0:e57bc370d339 148 */
rebonatto 0:e57bc370d339 149
rebonatto 0:e57bc370d339 150 //free(fft);
rebonatto 0:e57bc370d339 151
rebonatto 0:e57bc370d339 152 //printf("[3] %d %d %d %d\n", buffer[0], buffer[100], buffer[200], buffer[255]);
rebonatto 0:e57bc370d339 153 }
rebonatto 0:e57bc370d339 154
rebonatto 0:e57bc370d339 155 float* SignalProcessor::ComplexFFT(float* data, int sign, int ch)
rebonatto 0:e57bc370d339 156 {
rebonatto 0:e57bc370d339 157
rebonatto 0:e57bc370d339 158 //variables for the fft
rebonatto 0:e57bc370d339 159 unsigned long n,mmax,m,j,istep,i, met;
rebonatto 0:e57bc370d339 160 //double wtemp,wr,wpr,wpi,wi,theta,tempr,tempi;
rebonatto 0:e57bc370d339 161 float wtemp,wr,wpr,wpi,wi,theta,tempr,tempi;
rebonatto 0:e57bc370d339 162 //float *vector;
rebonatto 0:e57bc370d339 163 //the complex array is real+complex so the array
rebonatto 0:e57bc370d339 164 //as a size n = 2* number of complex samples
rebonatto 0:e57bc370d339 165 //real part is the data[index] and
rebonatto 0:e57bc370d339 166 //the complex part is the data[index+1]
rebonatto 0:e57bc370d339 167
rebonatto 0:e57bc370d339 168 /*
rebonatto 0:e57bc370d339 169 printf("Original\n");
rebonatto 0:e57bc370d339 170 for(int i=0; i < NUMBER_OF_SAMPLES; i++)
rebonatto 0:e57bc370d339 171 printf("%.2f ", data[i]);
rebonatto 0:e57bc370d339 172 printf("\n");
rebonatto 0:e57bc370d339 173 */
rebonatto 0:e57bc370d339 174
rebonatto 0:e57bc370d339 175 //new complex array of size n=2*sample_rate
rebonatto 0:e57bc370d339 176 //if(vector==0)
rebonatto 0:e57bc370d339 177 //vector=(float*)malloc(2*SAMPLE_RATE*sizeof(float)); era assim, define estava em Capture.h
rebonatto 0:e57bc370d339 178
rebonatto 0:e57bc370d339 179 //printf("Antes malloc\n");
rebonatto 0:e57bc370d339 180 /*
rebonatto 0:e57bc370d339 181 vector=(float*)malloc(2*NUMBER_OF_SAMPLES*sizeof(float));
rebonatto 0:e57bc370d339 182 memset(vector,0,2*NUMBER_OF_SAMPLES*sizeof(float));
rebonatto 0:e57bc370d339 183 */
rebonatto 0:e57bc370d339 184 //printf("DEpois malloc\n");
rebonatto 0:e57bc370d339 185
rebonatto 0:e57bc370d339 186 // DisplayRAMBanks();
rebonatto 0:e57bc370d339 187
rebonatto 0:e57bc370d339 188 //put the real array in a complex array
rebonatto 0:e57bc370d339 189 //the complex part is filled with 0's
rebonatto 0:e57bc370d339 190 //the remaining vector with no data is filled with 0's
rebonatto 0:e57bc370d339 191 //for(n=0; n<SAMPLE_RATE;n++)era assim, define estava em Capture.h
rebonatto 0:e57bc370d339 192
rebonatto 0:e57bc370d339 193 /*
rebonatto 0:e57bc370d339 194 for(n=0; n<NUMBER_OF_SAMPLES;n++)
rebonatto 0:e57bc370d339 195 {
rebonatto 0:e57bc370d339 196 if(n<NUMBER_OF_SAMPLES){
rebonatto 0:e57bc370d339 197 //vector[2*n]= (float) ( (data[n] - Settings::get_Offset(ch)) / Settings::get_Gain(ch) );
rebonatto 0:e57bc370d339 198 vector[2*n]= (float) data[n] ;
rebonatto 0:e57bc370d339 199 // printf("%.4f$", vector[2*n]);
rebonatto 0:e57bc370d339 200 }
rebonatto 0:e57bc370d339 201 else
rebonatto 0:e57bc370d339 202 vector[2*n]=0;
rebonatto 0:e57bc370d339 203 vector[2*n+1]=0;
rebonatto 0:e57bc370d339 204 }
rebonatto 0:e57bc370d339 205 */
rebonatto 0:e57bc370d339 206 /* trazendo o vetor em float, com metade ocupada, mudança dos valores reais e complexos
rebonatto 0:e57bc370d339 207 //put the real array in a complex array
rebonatto 0:e57bc370d339 208 //the complex part is filled with 0's
rebonatto 0:e57bc370d339 209 //the remaining vector with no data is filled with 0's
rebonatto 0:e57bc370d339 210 */
rebonatto 0:e57bc370d339 211 /*
rebonatto 0:e57bc370d339 212 printf("Original\n");
rebonatto 0:e57bc370d339 213 for(int i=0; i < NUMBER_OF_SAMPLES*2; i++)
rebonatto 0:e57bc370d339 214 printf("%.2f ", data[i]);
rebonatto 0:e57bc370d339 215 printf("\n");
rebonatto 0:e57bc370d339 216 */
rebonatto 0:e57bc370d339 217 met=NUMBER_OF_SAMPLES-1;
rebonatto 0:e57bc370d339 218 for(n=NUMBER_OF_SAMPLES*2-1; n > 0; n--){
rebonatto 0:e57bc370d339 219 if (n % 2 == 0){
rebonatto 0:e57bc370d339 220 data[n] = data[met];
rebonatto 0:e57bc370d339 221 met--;
rebonatto 0:e57bc370d339 222 }
rebonatto 0:e57bc370d339 223 else
rebonatto 0:e57bc370d339 224 data[n] = 0;
rebonatto 0:e57bc370d339 225 }
rebonatto 0:e57bc370d339 226
rebonatto 0:e57bc370d339 227 /*
rebonatto 0:e57bc370d339 228 printf("Modificado\n");
rebonatto 0:e57bc370d339 229 for(int i=0; i < NUMBER_OF_SAMPLES*2; i++)
rebonatto 0:e57bc370d339 230 printf("%.2f ", data[i]);
rebonatto 0:e57bc370d339 231 printf("\n");
rebonatto 0:e57bc370d339 232 */
rebonatto 0:e57bc370d339 233
rebonatto 0:e57bc370d339 234 //printf("[0] %.2f [100] %.2f [201] %.2f [255] %.2f\n", data[0], data[100], data[201], data[255]);
rebonatto 0:e57bc370d339 235
rebonatto 0:e57bc370d339 236 //binary inversion (note that the indexes
rebonatto 0:e57bc370d339 237 //start from 0 witch means that the
rebonatto 0:e57bc370d339 238 //real part of the complex is on the even-indexes
rebonatto 0:e57bc370d339 239 //and the complex part is on the odd-indexes)
rebonatto 0:e57bc370d339 240 //n=SAMPLE_RATE << 1; //multiply by 2era assim, define estava em Capture.h
rebonatto 0:e57bc370d339 241 n=NUMBER_OF_SAMPLES << 1; //multiply by 2
rebonatto 0:e57bc370d339 242 j=0;
rebonatto 0:e57bc370d339 243 for (i=0;i<n/2;i+=2) {
rebonatto 0:e57bc370d339 244 if (j > i) {
rebonatto 0:e57bc370d339 245 SWAP(data[j],data[i]);
rebonatto 0:e57bc370d339 246 SWAP(data[j+1],data[i+1]);
rebonatto 0:e57bc370d339 247 if((j/2)<(n/4)){
rebonatto 0:e57bc370d339 248 SWAP(data[(n-(i+2))],data[(n-(j+2))]);
rebonatto 0:e57bc370d339 249 SWAP(data[(n-(i+2))+1],data[(n-(j+2))+1]);
rebonatto 0:e57bc370d339 250 }
rebonatto 0:e57bc370d339 251 }
rebonatto 0:e57bc370d339 252 m=n >> 1;
rebonatto 0:e57bc370d339 253 while (m >= 2 && j >= m) {
rebonatto 0:e57bc370d339 254 j -= m;
rebonatto 0:e57bc370d339 255 m >>= 1;
rebonatto 0:e57bc370d339 256 }
rebonatto 0:e57bc370d339 257 j += m;
rebonatto 0:e57bc370d339 258 }
rebonatto 0:e57bc370d339 259 //end of the bit-reversed order algorithm
rebonatto 0:e57bc370d339 260
rebonatto 0:e57bc370d339 261 //Danielson-Lanzcos routine
rebonatto 0:e57bc370d339 262 mmax=2;
rebonatto 0:e57bc370d339 263 while (n > mmax) {
rebonatto 0:e57bc370d339 264 istep=mmax << 1;
rebonatto 0:e57bc370d339 265 theta=sign*(2*PI/mmax);
rebonatto 0:e57bc370d339 266 wtemp=sin(0.5*theta);
rebonatto 0:e57bc370d339 267 wpr = -2.0*wtemp*wtemp;
rebonatto 0:e57bc370d339 268 wpi=sin(theta);
rebonatto 0:e57bc370d339 269 wr=1.0;
rebonatto 0:e57bc370d339 270 wi=0.0;
rebonatto 0:e57bc370d339 271 for (m=1;m<mmax;m+=2) {
rebonatto 0:e57bc370d339 272 for (i=m;i<=n;i+=istep) {
rebonatto 0:e57bc370d339 273 j=i+mmax;
rebonatto 0:e57bc370d339 274 tempr=wr*data[j-1]-wi*data[j];
rebonatto 0:e57bc370d339 275 tempi=wr*data[j]+wi*data[j-1];
rebonatto 0:e57bc370d339 276 data[j-1]=data[i-1]-tempr;
rebonatto 0:e57bc370d339 277 data[j]=data[i]-tempi;
rebonatto 0:e57bc370d339 278 data[i-1] += tempr;
rebonatto 0:e57bc370d339 279 data[i] += tempi;
rebonatto 0:e57bc370d339 280 }
rebonatto 0:e57bc370d339 281 wr=(wtemp=wr)*wpr-wi*wpi+wr;
rebonatto 0:e57bc370d339 282 wi=wi*wpr+wtemp*wpi+wi;
rebonatto 0:e57bc370d339 283 }
rebonatto 0:e57bc370d339 284 mmax=istep;
rebonatto 0:e57bc370d339 285 }
rebonatto 0:e57bc370d339 286 //end of the algorithm
rebonatto 0:e57bc370d339 287
rebonatto 0:e57bc370d339 288
rebonatto 0:e57bc370d339 289 // Ajustes a FFT
rebonatto 0:e57bc370d339 290 for(i = 0; i < NUMBER_OF_SAMPLES*2; i++ ){
rebonatto 0:e57bc370d339 291 data[i] = (float) ((2 * data[i]) / NUMBER_OF_SAMPLES );
rebonatto 0:e57bc370d339 292
rebonatto 0:e57bc370d339 293 if (i % 2 == 1)
rebonatto 0:e57bc370d339 294 data[i] = data[i] * -1;
rebonatto 0:e57bc370d339 295 }
rebonatto 0:e57bc370d339 296
rebonatto 0:e57bc370d339 297 //printf("[2] %d %d %d %d\n", data[0], data[100], data[200], data[255]);
rebonatto 0:e57bc370d339 298 /*
rebonatto 0:e57bc370d339 299 printf("Na funcao\n");
rebonatto 0:e57bc370d339 300 for(int i=1;i<Settings::get_MaxHarmonics()*2+1;i++)
rebonatto 0:e57bc370d339 301 {
rebonatto 0:e57bc370d339 302 printf("[%dHz]\tsen %.4f\tcos %.4f\n", i*60, data[i*2+1], data[i*2]);
rebonatto 0:e57bc370d339 303 }
rebonatto 0:e57bc370d339 304 */
rebonatto 0:e57bc370d339 305 return NULL;
rebonatto 0:e57bc370d339 306 }
rebonatto 0:e57bc370d339 307
rebonatto 0:e57bc370d339 308 float SignalProcessor::ComplexFFTTeste(unsigned short int* data, float *vector, int sign, int ch)
rebonatto 0:e57bc370d339 309 {
rebonatto 0:e57bc370d339 310
rebonatto 0:e57bc370d339 311 //variables for the fft
rebonatto 0:e57bc370d339 312 unsigned long n,mmax,m,j,istep,i;
rebonatto 0:e57bc370d339 313 //double wtemp,wr,wpr,wpi,wi,theta,tempr,tempi;
rebonatto 0:e57bc370d339 314 float wtemp,wr,wpr,wpi,wi,theta,tempr,tempi;
rebonatto 0:e57bc370d339 315 //float *vector;
rebonatto 0:e57bc370d339 316 //the complex array is real+complex so the array
rebonatto 0:e57bc370d339 317 //as a size n = 2* number of complex samples
rebonatto 0:e57bc370d339 318 //real part is the data[index] and
rebonatto 0:e57bc370d339 319 //the complex part is the data[index+1]
rebonatto 0:e57bc370d339 320
rebonatto 0:e57bc370d339 321 //new complex array of size n=2*sample_rate
rebonatto 0:e57bc370d339 322 //if(vector==0)
rebonatto 0:e57bc370d339 323 //vector=(float*)malloc(2*SAMPLE_RATE*sizeof(float)); era assim, define estava em Capture.h
rebonatto 0:e57bc370d339 324
rebonatto 0:e57bc370d339 325 //printf("Antes malloc\n");
rebonatto 0:e57bc370d339 326 //vector=(float*)malloc(2*Settings::get_Samples()*sizeof(float));
rebonatto 0:e57bc370d339 327 memset(vector,0,2*NUMBER_OF_SAMPLES*sizeof(float));
rebonatto 0:e57bc370d339 328 //printf("DEpois memset\n");
rebonatto 0:e57bc370d339 329
rebonatto 0:e57bc370d339 330
rebonatto 0:e57bc370d339 331 DisplayRAMBanks();
rebonatto 0:e57bc370d339 332
rebonatto 0:e57bc370d339 333 //put the real array in a complex array
rebonatto 0:e57bc370d339 334 //the complex part is filled with 0's
rebonatto 0:e57bc370d339 335 //the remaining vector with no data is filled with 0's
rebonatto 0:e57bc370d339 336 //for(n=0; n<SAMPLE_RATE;n++)era assim, define estava em Capture.h
rebonatto 0:e57bc370d339 337
rebonatto 0:e57bc370d339 338 for(n=0; n<NUMBER_OF_SAMPLES;n++)
rebonatto 0:e57bc370d339 339 {
rebonatto 0:e57bc370d339 340 if(n<NUMBER_OF_SAMPLES){
rebonatto 0:e57bc370d339 341 //vector[2*n]= (float) ( (data[n] - Settings::get_Offset(ch)) / Settings::get_Gain(ch) );
rebonatto 0:e57bc370d339 342 vector[2*n]= (float) data[n] ;
rebonatto 0:e57bc370d339 343 // printf("%.4f$", vector[2*n]);
rebonatto 0:e57bc370d339 344 }
rebonatto 0:e57bc370d339 345 else
rebonatto 0:e57bc370d339 346 vector[2*n]=0;
rebonatto 0:e57bc370d339 347 vector[2*n+1]=0;
rebonatto 0:e57bc370d339 348 }
rebonatto 0:e57bc370d339 349
rebonatto 0:e57bc370d339 350 //printf("Passou primeiro lcao\n");
rebonatto 0:e57bc370d339 351
rebonatto 0:e57bc370d339 352 //printf("[1] %d %d %d %d\n", data[0], data[100], data[200], data[255]);
rebonatto 0:e57bc370d339 353
rebonatto 0:e57bc370d339 354 //binary inversion (note that the indexes
rebonatto 0:e57bc370d339 355 //start from 0 witch means that the
rebonatto 0:e57bc370d339 356 //real part of the complex is on the even-indexes
rebonatto 0:e57bc370d339 357 //and the complex part is on the odd-indexes)
rebonatto 0:e57bc370d339 358 //n=SAMPLE_RATE << 1; //multiply by 2era assim, define estava em Capture.h
rebonatto 0:e57bc370d339 359 n=NUMBER_OF_SAMPLES << 1; //multiply by 2
rebonatto 0:e57bc370d339 360 j=0;
rebonatto 0:e57bc370d339 361 for (i=0;i<n/2;i+=2) {
rebonatto 0:e57bc370d339 362 if (j > i) {
rebonatto 0:e57bc370d339 363 SWAP(vector[j],vector[i]);
rebonatto 0:e57bc370d339 364 SWAP(vector[j+1],vector[i+1]);
rebonatto 0:e57bc370d339 365 if((j/2)<(n/4)){
rebonatto 0:e57bc370d339 366 SWAP(vector[(n-(i+2))],vector[(n-(j+2))]);
rebonatto 0:e57bc370d339 367 SWAP(vector[(n-(i+2))+1],vector[(n-(j+2))+1]);
rebonatto 0:e57bc370d339 368 }
rebonatto 0:e57bc370d339 369 }
rebonatto 0:e57bc370d339 370 m=n >> 1;
rebonatto 0:e57bc370d339 371 while (m >= 2 && j >= m) {
rebonatto 0:e57bc370d339 372 j -= m;
rebonatto 0:e57bc370d339 373 m >>= 1;
rebonatto 0:e57bc370d339 374 }
rebonatto 0:e57bc370d339 375 j += m;
rebonatto 0:e57bc370d339 376 }
rebonatto 0:e57bc370d339 377 //end of the bit-reversed order algorithm
rebonatto 0:e57bc370d339 378
rebonatto 0:e57bc370d339 379 printf("Passou Segundo lcao\n");
rebonatto 0:e57bc370d339 380
rebonatto 0:e57bc370d339 381 //Danielson-Lanzcos routine
rebonatto 0:e57bc370d339 382 mmax=2;
rebonatto 0:e57bc370d339 383 while (n > mmax) {
rebonatto 0:e57bc370d339 384 istep=mmax << 1;
rebonatto 0:e57bc370d339 385 theta=sign*(2*PI/mmax);
rebonatto 0:e57bc370d339 386 wtemp=sin(0.5*theta);
rebonatto 0:e57bc370d339 387 wpr = -2.0*wtemp*wtemp;
rebonatto 0:e57bc370d339 388 wpi=sin(theta);
rebonatto 0:e57bc370d339 389 wr=1.0;
rebonatto 0:e57bc370d339 390 wi=0.0;
rebonatto 0:e57bc370d339 391 for (m=1;m<mmax;m+=2) {
rebonatto 0:e57bc370d339 392 for (i=m;i<=n;i+=istep) {
rebonatto 0:e57bc370d339 393 j=i+mmax;
rebonatto 0:e57bc370d339 394 tempr=wr*vector[j-1]-wi*vector[j];
rebonatto 0:e57bc370d339 395 tempi=wr*vector[j]+wi*vector[j-1];
rebonatto 0:e57bc370d339 396 vector[j-1]=vector[i-1]-tempr;
rebonatto 0:e57bc370d339 397 vector[j]=vector[i]-tempi;
rebonatto 0:e57bc370d339 398 vector[i-1] += tempr;
rebonatto 0:e57bc370d339 399 vector[i] += tempi;
rebonatto 0:e57bc370d339 400 }
rebonatto 0:e57bc370d339 401 wr=(wtemp=wr)*wpr-wi*wpi+wr;
rebonatto 0:e57bc370d339 402 wi=wi*wpr+wtemp*wpi+wi;
rebonatto 0:e57bc370d339 403 }
rebonatto 0:e57bc370d339 404 mmax=istep;
rebonatto 0:e57bc370d339 405 }
rebonatto 0:e57bc370d339 406 //end of the algorithm
rebonatto 0:e57bc370d339 407
rebonatto 0:e57bc370d339 408 printf("Fim FFT\n");
rebonatto 0:e57bc370d339 409 /*
rebonatto 0:e57bc370d339 410 // Ajustes a FFT
rebonatto 0:e57bc370d339 411 for(i = 0; i < Settings::get_Samples()*2; i++ ){
rebonatto 0:e57bc370d339 412 vector[i] = (float) ((2 * vector[i]) / Settings::get_Samples() );
rebonatto 0:e57bc370d339 413
rebonatto 0:e57bc370d339 414 if (i % 2 == 1)
rebonatto 0:e57bc370d339 415 vector[i] = vector[i] * -1;
rebonatto 0:e57bc370d339 416
rebonatto 0:e57bc370d339 417 }
rebonatto 0:e57bc370d339 418 */
rebonatto 0:e57bc370d339 419 //printf("[2] %d %d %d %d\n", data[0], data[100], data[200], data[255]);
rebonatto 0:e57bc370d339 420
rebonatto 0:e57bc370d339 421 return vector[0];
rebonatto 0:e57bc370d339 422 }
rebonatto 0:e57bc370d339 423
rebonatto 0:e57bc370d339 424
rebonatto 0:e57bc370d339 425 /*
rebonatto 0:e57bc370d339 426 float SignalProcessor::DFT(float *data, float *seno, float *coss){
rebonatto 0:e57bc370d339 427 int i, j;
rebonatto 0:e57bc370d339 428
rebonatto 0:e57bc370d339 429 for(i=0; i < Settings::get_MaxHarmonics()+1; i++)
rebonatto 0:e57bc370d339 430 seno[i] = coss[i] = 0;
rebonatto 0:e57bc370d339 431
rebonatto 0:e57bc370d339 432 for(i=0; i < Settings::get_Samples(); i++){
rebonatto 0:e57bc370d339 433 for(j = 0; j < Settings::get_MaxHarmonics()+1; j++ ){
rebonatto 0:e57bc370d339 434 coss[j] += (data[i] * (cos( (2 * PI * i * j) / Settings::get_Samples() ) ) ) ;
rebonatto 0:e57bc370d339 435 seno[j] += (data[i] * (sin( (2 * PI * i * j) / Settings::get_Samples() ) ) ) ;
rebonatto 0:e57bc370d339 436 }
rebonatto 0:e57bc370d339 437 }
rebonatto 0:e57bc370d339 438
rebonatto 0:e57bc370d339 439 for(j = 1; j < Settings::get_MaxHarmonics()+1; j++ ){
rebonatto 0:e57bc370d339 440 coss[j] = 2 * coss[j] / Settings::get_Samples();
rebonatto 0:e57bc370d339 441 seno[j] = 2 * seno[j] / Settings::get_Samples() ;
rebonatto 0:e57bc370d339 442 }
rebonatto 0:e57bc370d339 443 return (float) (coss[0] / Settings::get_Samples()) + (seno[0] / Settings::get_Samples());
rebonatto 0:e57bc370d339 444 }
rebonatto 0:e57bc370d339 445 */
rebonatto 0:e57bc370d339 446