Mohsen Samadani
/
Algorithm-testing
Code for measuring the signal with a specified length and sampling rate, and saving it on a SD card.
Diff: main.cpp
- Revision:
- 0:c05b00be2229
- Child:
- 1:a514e4de034d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jul 13 19:33:37 2017 +0000 @@ -0,0 +1,140 @@ +#include "main.h" +#include <string> +#include "mbed.h" +#include "sdram.h" +#include "SPIFI.h" +#include "stdio.h" +#include "string.h" +#include "math.h" +#include "stdlib.h" +#include "MCIFileSystem.h" +#include "measureSignal.h" +#include "savePower.h" +#include "sdcard.h" +#include "diagnosis.h" +#include <algorithm> + + +#include "rt_nonfinite.h" +#include "bpFilter.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; +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() +{ + + /* -----------------Initial setup -----------------------*/ + pc.baud(115200); // set baud rate for serial port. + sdram_init(); // initialize SDRAM. + //savePower(); //save power by turning ethernet and SDRAM off. + + + /*-----------------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-----------------------------*/ + printf("Performing analysis...\n"); + emxArray_real32_T *signal2; + static int iv0[] = { xsize,1 }; + signal2 = emxCreateND_real32_T(1, *(int (*)[1])&iv0[0]); + 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); + + // Call the entry-point 'bpFilter'. + bpFilter(signal2, bpSignal); + + + // Print data to terminal + + for (int i = 0; i < 50; i++) { + printf("%f %f %f\n",signal[i],signal2->data[i],bpSignal->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"); + + + return 0; +} \ No newline at end of file