--- /dev/null
+/*---------------------------------------------------------------------------*\\r
+\r
+ FILE........: adc_sd.c\r
+ AUTHOR......: David Rowe\r
+ DATE CREATED: 30 May 2014\r
+\r
+ Measures the std deviation of the ADC signals. Used to check noise\r
+ levels on each ADC.\r
+\r
+\*---------------------------------------------------------------------------*/\r
+\r
+/*\r
+ Copyright (C) 2014 David Rowe\r
+\r
+ All rights reserved.\r
+\r
+ This program is free software; you can redistribute it and/or modify\r
+ it under the terms of the GNU Lesser General Public License version 2.1, as\r
+ published by the Free Software Foundation. This program is\r
+ distributed in the hope that it will be useful, but WITHOUT ANY\r
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\r
+ License for more details.\r
+\r
+ You should have received a copy of the GNU Lesser General Public License\r
+ along with this program; if not, see <http://www.gnu.org/licenses/>.\r
+*/\r
+\r
+#include <stdlib.h>\r
+#include <math.h>\r
+#include "stm32f4_adc.h"\r
+#include "stm32f4_dac.h"\r
+#include "gdb_stdio.h"\r
+\r
+#define REC_TIME_SECS 10\r
+#define N (ADC_BUF_SZ*4)\r
+#define FS 16000\r
+\r
+static float calc_sd(short x[], int n) {\r
+ float sum, mean, sum_diff, sd;\r
+ int i;\r
+\r
+ sum = 0.0;\r
+ for(i=0; i<n;i++) {\r
+ sum += (float)x[i];\r
+ }\r
+ mean = sum/n;\r
+\r
+ sum_diff = 0.0;\r
+ for(i=0; i<n;i++) {\r
+ sum_diff += ((float)x[i] - mean)*((float)x[i] - mean);\r
+ }\r
+ \r
+ sd = sqrtf(sum_diff/n);\r
+\r
+ return sd;\r
+}\r
+\r
+int main(void){\r
+ short buf[N];\r
+ float sd1, sd2;\r
+\r
+ adc_open(2*N);\r
+\r
+ printf("Starting!\n");\r
+ while(1) {\r
+ while(adc1_read(buf, N) == -1);\r
+ sd1 = calc_sd(buf, N);\r
+ while(adc2_read(buf, N) == -1);\r
+ sd2 = calc_sd(buf, N);\r
+\r
+ printf("adc1: %5.1f adc2: %5.1f\n", (double)sd1, (double)sd2);\r
+ }\r
+\r
+}\r