Sending out an analog signal containing digital data is a waste of time. You would only do that if you needed to actually use the analog input (say, for a microphone) but you wanted to test with a known input generated from Matlab.
Matlab can talk to COM ports. It can send the data out one sample at a time, then wait for the result to come back. Have Matlab store the value or even use its charting functions.
If you wanted to use Processing for this, you can't have Matlab send the data direct to the COM port, because you can't split transmit and receive to different programs. Just make a data file with the data from Matlab and have Processing read it out to the Arduino then receive the result.
Well, I've already shown an example of the FIR filter. Here's a complete working example of an IIR.
/*********************************************************************
* Single Pole IIR filter
* Author: Morgan
* Date: 26 Sept 2017
*
* Purpose: Demonstrate a working IIR filter using Serial
********************************************************************/
//Serial.parseFloat is useful, but distinguishing a timeout from a real number is difficult
//We need to explicitly set its timeout (so we know what it is) and then see if it took that long
//If it did take that long, then we can assume that there was no number.
#define MyTimeout 500 //milliseconds
void setup() {
Serial.begin(9600);
while(!Serial && millis()<5000) {
//wait up to 5 sec for Serial connection (Teensy/Leonardo only)
}
Serial.println("IIR filter demo version 1.0");
Serial.println("Give me numbers, one on each line and I'll give you an IIR filtered output (one on each line.)");
Serial.setTimeout(MyTimeout);
}
void loop() {
unsigned long StartTime = millis();
float inputVal = Serial.parseFloat();
if(inputVal !=0 || millis() - StartTime < MyTimeout) {
//we got a valid input from parseFloat()
float outputVal = filter(inputVal);
Serial.println(outputVal);
}
}
float filter(float inputVal) {
//implement the IIR filter
//reference: The Scientist and Engineer's Guide to Digital Signal Processing By Steven W. Smith, Ph.D.
// http://www.dspguide.com/ch19/2.htm
//This is a "single pole" filter.
//x is the decay between adjacent samples. Choose any number between 0 and 1 (except 1).
const float x = 0.86;
static float outputVal = 0; //make it static so it's stored for the function to use again the next time it's called
outputVal = outputVal * x + inputVal * (1-x);
return outputVal;
}
There's some complexity in this to get around the limitations of the parseFloat() method but the rest of it should be easy enough to understand.