Code for measuring the signal with a specified length and sampling rate, and saving it on a SD card.

Dependencies:   EALib I2S mbed

Revision:
1:a514e4de034d
Parent:
0:c05b00be2229
Child:
2:8c5b6522139f
--- a/main.cpp	Thu Jul 13 19:33:37 2017 +0000
+++ b/main.cpp	Fri Jul 14 14:41:10 2017 +0000
@@ -11,63 +11,26 @@
 #include "measureSignal.h"
 #include "savePower.h"
 #include "sdcard.h"
-#include "diagnosis.h"
 #include <algorithm>
 
-
-#include "rt_nonfinite.h"
-#include "bpFilter.h"
+#include "bandPass.h"
+#include "envSignal.h"
+#include "envSignal_emxAPI.h"
+#include "envSignal_emxutil.h"
+#include "envSignal_types.h"
 #include "main.h"
-#include "bpFilter_terminate.h"
-#include "bpFilter_emxAPI.h"
-#include "bpFilter_initialize.h"
-
 
 static emxArray_real32_T *argInit_Unboundedx1_real32_T(float* x);
 
 /* Define variables */
-int fs=33000;   // sampling rate
-//int N=297000;   // original signal length
-int xsize=33000;//262144;
-int transient=000;
+int fs=330000;   // sampling rate
+int xsize=33000;// signal length;
+int transient=0; // transient part that is cut from the signal
 std::string filename = "noisetest2";  // name of file saved to SD card
 
 
 Serial pc(USBTX, USBRX);
 
-float average(int numbers[], int size)
-{
-    float sum = 0.0;
-    for (int x = 0; x < size; x++) {
-        sum += (float)numbers[x];
-    }
-    return sum /(float)size;
-}
-
-
-static emxArray_real32_T *argInit_Unboundedx1_real32_T(float* x)
-{
-    emxArray_real32_T *result;
-    static int iv0[1] = { 2 };
-
-    int idx0;
-
-    // Set the size of the array.
-    // Change this size to the value that the application requires.
-    result = emxCreateND_real32_T(1, *(int (*)[1])&iv0[0]);
-
-    // Loop over the array to initialize each element.
-    for (idx0 = 0; idx0 < result->size[0U]; idx0++) {
-        // Set the value of the array element.
-        // Change this value to the value that the application requires.
-        result->data[idx0] = x[idx0];
-    }
-
-    return result;
-}
-
-
-
 
 int main()
 {
@@ -79,61 +42,54 @@
 
 
     /*-----------------Record Signal with Codec---------------------*/
+   
     int N=xsize+transient; //length of the original signal
     int *signal_raw = (int *)malloc(N*sizeof(int));
     measureSignal(signal_raw,fs,N);  // measure a signal with sampling frequency of fs and lenght of N
 
     // Scale data and convert to float
 
-    printf("Scaling data...\n");
     float *signal = (float *)malloc(xsize*sizeof(float));
     for (int i = 0; i < xsize; i++) {
         signal[i]=(float)(signal_raw[i+transient])/(5.8*pow(2.0,24));
     }
     //free(signal_raw);
 
+    /*----------------Envelope Analysis-----------------------------*/
 
-    /*----------------Envelope Analysis-----------------------------*/
     printf("Performing analysis...\n");
     emxArray_real32_T *signal2;
-    static int iv0[] = { xsize,1 };
-    signal2 = emxCreateND_real32_T(1, *(int (*)[1])&iv0[0]);
+    emxArray_real32_T *fir_coef;
+    emxArray_real32_T *env_signal ;
+    
+    static int iv0[2] = { xsize,1 };
+    signal2 = emxCreateND_real32_T(1, iv0);
     for (int idx0 = 0; idx0 < xsize; idx0++) {
         signal2->data[idx0] = signal[idx0];
     }
-    //signal2 = argInit_Unboundedx1_real32_T(signal);
 
-    emxArray_real32_T *bpSignal ;
-  
-    emxInitArray_real32_T(&bpSignal, 1);
+    static int iv[2] = { 3201,1 };
+    fir_coef = emxCreateND_real32_T(1, iv);
+    for (int idx = 0; idx < 3201; idx++) {
+        fir_coef->data[idx] = fir_c[idx];
+    }
 
-    // Call the entry-point 'bpFilter'.
-    bpFilter(signal2, bpSignal);
+ 
+    emxInitArray_real32_T(&env_signal, 1);
+    envSignal(signal2,fir_coef, env_signal);
 
 
     // Print data to terminal
-
-    for (int i = 0; i < 50; i++) {
-        printf("%f            %f          %f\n",signal[i],signal2->data[i],bpSignal->data[i]);
-
+    printf("analysis done\n");
+    for (int i = 5000; i < 5200; i++) {
+        printf("%f            %f          %f\n",signal[i],signal2->data[i],env_signal->data[i]);
     }
 
-    /*
-    int max = *std::max_element(signal_raw, signal_raw + xsize);
-    int min = *std::min_element(signal_raw, signal_raw + xsize);
-    float Max=(float)max/(5.8*pow(2.0,24))*1000;
-    float Min=(float)min/(5.8*pow(2.0,24))*1000;
-    float Avg;
-    Avg=average(signal_raw,xsize)/(5.8*pow(2.0,24))*1000;
 
-    printf("Voltage: Min=%f   Avg=%f   Max=%f  (mv)\n",Min,Avg,Max);
-    printf("Acceleration: Min=%f   Avg=%f   Max=%f (g) \n",Min/8.8, Avg/8.8,Max/8.8);
-
-    */
     /*---------------------------------------------------------------*/
     //saveToSD(signal,xsize,filename.c_str());  //save data to SD card
-    saveToSD(signal,xsize,"signal.txt");
-    saveToSD(bpSignal->data,xsize,"bpsignal.txt");
+    // saveToSD(signal,xsize,"signal.txt");
+    // saveToSD(env_signal->data,xsize,"envsignal.txt");
 
 
     return 0;