Thanks again. I tried to implement the filter using the FIR Filter Arduino Library. Tests showed that the library seems to be working correctly using the examples with some real-time data. When I tried the filter from tfilter, I got an interesting 'enveloped' output with an amplitude of +/- 10E5 at steady state (see attachment), and +/-10E6 if waving. This is for 10-bit input data with 10 count noise @50Hz. See attached sketch and plot.
The time to process each data point was only 20us which I hope is correct (on a Mega2560). Can you recommend good filter design resources? I see this is a deep subject with lots of hits on google. I am also interested to try fft, this sounds more promising. Thanks.
The sketch is attached in my prev post between the 2 pictures "ALS-PT19_test_sketch.ino". BTW this was the first time I used SerialScope and I noticed the y-axis values sometimes rescale to different values although the data remains the same. After I 'rezeroed' SerialScope the filtered output was reaching ~5k @ s.s. and ~ 100k during waving. Also the step response plot is from when I set the gain to 1.
I copied your values into the sketch but I have a similar result, here are ss and waving plots, green is the moving average which seems to work well. How are you running the code? Could you send a pic of your result?
I was wondering if the behavior could be due to the nature of the filter which shows amplification of frequencies above 20Hz?
Sorry, I really have no idea what you are doing. You haven't posted enough information.
You MUST NOT allow signal components with frequencies higher than (sample frequency)/2 in the input data, or the output will be hopelessly aliased. Look up "Sample Theorem" and "Nyquist limit" to see why. When working with real data, you may need an analog low pass filter before the input becomes digitized.
I'm not sure what you meant about not knowing what I'm doing. I'm running the attached sketch with the filter coefficients you provided. The sketch samples the Mega2560 ADC at 50Hz and then provides the samples to 2 filters defined in the sketch, one 10-point moving average that seems to work and the FIR bandpass filter we are working on. For some reason the band pass filter doesn't appear to work right. Of course the signal has some noise in it, all signals do, in service there's going to be a lot of variation and noise, that's why I wanted the filter to filter everything out except the 2-6Hz frequency range. There are little tiny variations in the signal do you think these would be expected to cause the instability I see? You said the result is about what you expected, what input did you use? I attached some of my sensor data. I'll try to use the moving average output as the FIR input.
BTW I found this awesome free real-time serial signal fft app that is very practical easy to use user friendly and has many features: Serial Analyzer App. Highly recommend for Arduino users!
I have tested 6 sensors to examine their output: TS2561 digital lux, HC-SR04 ultrasonics, TS2591 digital lux, ALS-PT19 analog light, RCWL-0516 microwave radar, VL53L0X TOF. I tried to apply filtering by moving average and FIR bandpass filtering. Bandpass tests have resulted in unexpected results and I am investigating further. The digital light sensors are not sensitive enough in low light to allow a high enough sampling rate, about 10Hz is max. The analog light sensor has high sensitivity and can be sampled faster. 50Hz seems to work. The VL53L0X TOF sensor seems to be most promising at this point, with fast sampling available and less sensitive to ambient lighting conditions. I have seen other TOF sensors that have a narrower field of view I would like to try as the VL53L0X field of view is 25 deg. I haven't yet tested my Sharp GY IR sensor as I have experience with it and don't expect good results. I am taking a deep dive into DSP and band pass filtering at the moment. I thought I would just look at the amplitude of the filtered time data to determine detection. After looking at fft I think a better approach would be to do peak detection in the frequency domain. Any suggestions are welcome. Attached are 3 plots, the first 2 are the bandpass applied to sensor data which has moving average applied to it, steady, and waving. The 3rd plot is an fft of averaged then filtered data. The time series data is unwieldy but the fft is possibly useful.
jremington - I didn't realize you had included an entire sketch in this post. I had only copied the taps into my own sketch without seeing the rest. I am testing your sketch method now, thanks.
Here is a sweep showing the amplitude response of the bandpass filter code designed by tfilter (32 point rms amplitude versus f in Hz for sine wave input), with improved coefficients (below).
f = 1.00 rms = 1.23
f = 2.00 rms = 1.10
f = 3.00 rms = 18.23
f = 4.00 rms = 58.18
f = 5.00 rms = 82.08
f = 6.00 rms = 58.94
f = 7.00 rms = 17.84
f = 8.00 rms = 0.45
f = 9.00 rms = 0.75
f = 10.00 rms = 1.16
f = 11.00 rms = 0.41
f = 12.00 rms = 0.83
f = 13.00 rms = 0.88
f = 14.00 rms = 0.12
f = 15.00 rms = 0.87
f = 16.00 rms = 0.64
f = 17.00 rms = 0.29
f = 18.00 rms = 0.85
f = 19.00 rms = 0.40
f = 20.00 rms = 0.47
f = 21.00 rms = 0.79
f = 22.00 rms = 0.18
f = 23.00 rms = 0.60
f = 24.00 rms = 0.68