ECE 486 Lab Assignments
ece486_running_mean.c
Go to the documentation of this file.
1 /*!
2  * @file
3  *
4  * @brief Running Mean Filter Implementation
5  *
6  * @author ECE486 Lab Group 0
7  * @author Don Hummels
8  *
9  * @date Jan 2022
10  */
11 
12 
13 
14 #include <stdlib.h>
15 #include "ece486_running_mean.h"
16 
17 
18 /*
19  * build the RUNNING_MEAN_T structure...
20  */
22  int M,
23  int blocksize )
24 {
25  RUNNING_MEAN_T *s;
26  int i;
27 
28  s = (RUNNING_MEAN_T *)malloc( sizeof(RUNNING_MEAN_T) );
29  s->M = M;
30  s->blocksize = blocksize;
31 
32  s->history = (float *) malloc ( M * sizeof(float) );
33  for (i=0; i<M; i++) s->history[i] = 0.0f;
34 
35  return(s);
36 }
37 
38 /*
39  * Calculate a filter output samples from a block of input data
40  *
41  * Note... This is not a particularly efficient way to implement a running mean filter. It is meant
42  * simply to provide some test code that returns a (hopefully) correct answer.
43  *
44  */
45 void calc_running_mean( RUNNING_MEAN_T *s, float *x_in, float *y_out)
46 {
47  int n; /* Time index for output sample calculation */
48  int i;
49 
50  for (n=0; n< s->blocksize; n++) {
51  // Update the "history" buffer to contain the most recent M samples
52  // Current and previous samples are arranged in the history buffer by
53  // s->history[0] = x_in[n]
54  // s->history[1] = x_in[n-1]
55  // s->history[2] = x_in[n-2]
56  // ...
57  // s->history[M-1] = x_in[n-M+1]
58  //
59  for (i=s->M-1; i>0; i--) {
60  s->history[i] = s->history[i-1];
61  }
62  s->history[0] = x_in[n];
63 
64  // Get the nth filter output by averaging the samples in the history buffer
65  y_out[n] = 0.0f;
66  for (i=0; i<s->M; i++) y_out[n] += s->history[i];
67  y_out[n] /= (float) s->M;
68  }
69 
70 }
71 
72 /*
73  * Release previously allocated memory
74  */
76 {
77  free(s->history);
78  free(s);
79 }
80 
ece486_running_mean.h
FIR filter using fixed-length input sample blocks.
calc_running_mean
void calc_running_mean(RUNNING_MEAN_T *s, float *x_in, float *y_out)
Calculate a block of output samples of a running meain filter from an input sample block.
Definition: ece486_running_mean.c:45
running_mean::M
int M
Definition: ece486_running_mean.h:77
running_mean
filter structure containing the filter state
Definition: ece486_running_mean.h:76
destroy_running_mean
void destroy_running_mean(RUNNING_MEAN_T *s)
Release memory associated with an #FIR_T.
Definition: ece486_running_mean.c:75
init_running_mean
RUNNING_MEAN_T * init_running_mean(int M, int blocksize)
running mean filter initialization
Definition: ece486_running_mean.c:21
running_mean::blocksize
int blocksize
Definition: ece486_running_mean.h:78
running_mean::history
float * history
Definition: ece486_running_mean.h:79