In embedded signal processing, raw sensor data is often noisy. A common technique to smooth this data is a Moving Average Filter. Implementing a separate filter class for every data type (int, float, double) and window size results in code duplication. Your task is to implement a Class Template named MovingAverage. It must accept:
typename T: The data type of the samples (e.g., float, int).int WindowSize: The number of samples to average (Non-Type Template Parameter).The class must maintain a circular buffer (array) of WindowSize elements. Implement the method:
T addSample(T sample):Program Flow:
main function instantiates a MovingAverage<float, 4> (Window Size = 4).N (number of inputs).N times.sample.filter.addSample(sample).Input Format:
N.N lines: Float sample.Output Format:
Avg: <value>10.50).Example:
Example 1 (Window Size 4)
Input:
5
10.0
20.0
30.0
40.0
50.0Output:
Avg: 10.00
Avg: 15.00
Avg: 20.00
Avg: 25.00
Avg: 35.00Explanation:
Constraints:
N range: 1 to 20WindowSize fixed at 4 for tests.new, malloc).
Input
5 10.0 20.0 30.0 40.0 50.0
Expected Output
Avg: 10.00 Avg: 15.00 Avg: 20.00 Avg: 25.00 Avg: 35.00