protegemed, aquisição via A/D simples utilizando interrupção do timer

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Fork of ptgm_semDMA by Marcelo Rebonatto

Committer:
rebonatto
Date:
Tue Jan 05 11:47:35 2016 +0000
Revision:
0:fac116e94d44
Vers?o est?vel sem DMA e FFT. 128 amostras.

Who changed what in which revision?

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