|
| 1 | +/* |
| 2 | + * Aya Helmy wrote this example for her 4B25 coursework. |
| 3 | + * James Meech adapted it to run over sunflower and take input from the sigsrc command. |
| 4 | + */ |
| 5 | +#include <stdio.h> |
| 6 | +#include <string.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <limits.h> |
| 9 | +#include <stdbool.h> |
| 10 | +#include "sf-types.h" |
| 11 | +#include "tag.h" |
| 12 | +#include "devsim7708.h" |
| 13 | +#include "sh7708.h" |
| 14 | +#include "devscc.h" |
| 15 | +#include "devrtc.h" |
| 16 | +#include "devexcp.h" |
| 17 | +#include "devlog.h" |
| 18 | +#include "devloc.h" |
| 19 | +#include "devsensor.h" |
| 20 | +#include "misc.h" |
| 21 | +#include "print.h" |
| 22 | + |
| 23 | +float LowPassFilter(float *in, float *out, int N, double in0) |
| 24 | +{ |
| 25 | +out[0] = in[0] + in0; |
| 26 | +for (int n=1; n < N ; n++) |
| 27 | +{ |
| 28 | +out[n] = in[n] + in[n-1]; |
| 29 | +} |
| 30 | +return in[N-1]; |
| 31 | +} |
| 32 | + |
| 33 | +int |
| 34 | +main(void) |
| 35 | +{ |
| 36 | +const int numberOfSamples = 30; |
| 37 | +int halfNumberOfSamples = numberOfSamples/2; |
| 38 | +float xBuffer[numberOfSamples]; |
| 39 | +float xBufferOut[numberOfSamples]; |
| 40 | +float in0 = 0; |
| 41 | +for(int j = 0; j < numberOfSamples; j++) |
| 42 | + { |
| 43 | +/* |
| 44 | + * Insert delay of 2500 uSeconds to simulate 400 Hz sample rate |
| 45 | + */ |
| 46 | +xudelay(2500); |
| 47 | +/* |
| 48 | + * Read sensor readings from sigsrc 0 which is the x-axis accelerometer readings |
| 49 | + */ |
| 50 | + xBuffer[j] = devsignal_read(0); |
| 51 | +/* |
| 52 | + * Please note that sigsrc simulates a real signal which changes in time. |
| 53 | + * The value returned by devsignal_read() will be different at different simulation times. |
| 54 | + * Here we use delay to wait for 1/f seconds where f is the 400 Hz sample frequency specified in the run.m file. |
| 55 | + * If we don't wait for this amount of time and instead sample as quickly as we can we will see the same sensor value many times. |
| 56 | + * If we set xudelay to a higher value we will start to miss some samples in the file as we are looking at the return value of i |
| 57 | + * devsignal_read() at a frequency lower than 400 Hz. |
| 58 | + */ |
| 59 | + } |
| 60 | + |
| 61 | +in0 = LowPassFilter(xBuffer, xBufferOut, halfNumberOfSamples, in0); |
| 62 | +in0 = LowPassFilter(&xBuffer[halfNumberOfSamples], &xBufferOut[halfNumberOfSamples], halfNumberOfSamples, in0); |
| 63 | + |
| 64 | +for(int i = 0; i < numberOfSamples; i++) |
| 65 | +{ |
| 66 | +printf("x[%d]=%f\ty[%d]=%f\n",i,xBuffer[i], i, xBufferOut[i]); |
| 67 | +} |
| 68 | + |
| 69 | +return 0; |
| 70 | +} |
| 71 | + |
0 commit comments