-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFIR_Example_API.c
More file actions
93 lines (74 loc) · 2.35 KB
/
FIR_Example_API.c
File metadata and controls
93 lines (74 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* FIRFilter.c
*
* Created on: 2021/11/05
* Author: johnson
*/
#include "FIRFilter.h"
#include <stdlib.h>
// Check if circular buffer is empty
// @param cbuf
// @return true if cbuf is empty, false if not empty
bool circular_buf_empty(circular_handle_t cbuf){
return(!cbuf->full && (cbuf->head == cbuf->tail));
};
// Check if circular buffer is full
// @param cbuf
// @return bool true if circular buffer is full, false if not full
bool circular_buf_full(circular_handle_t cbuf){
return cbuf->full;
};
// Initial circular buffer
// @param cbuf circular buffer pointer
// @param size buffer size
circular_handle_t circular_buf_init(uint16_t *buffer, uint16_t size){
circular_handle_t cbuf = malloc(sizeof(circular_buf_t));
cbuf->buffer = buffer;
cbuf->max = size;
cbuf->head = 0;
cbuf->tail = 0;
cbuf->full = false;
return cbuf;
};
// Increase circulae buffer pointer position
// @param cbuf
void increase_pointer(circular_handle_t cbuf){
// Increade head pointer
// If reach max capacity, reset to zero
if(++(cbuf->head) == cbuf->max)
cbuf->head = 0;
// Increase tail pointer if buffer is full
// If reach max capacity, reset to zero
if(cbuf->full){
if(++(cbuf->tail) == cbuf-> max)
cbuf->tail = 0;
};
// If head == tail means buffer is full
// set the flag to true
cbuf->full = (cbuf->head == cbuf->tail);
};
// Put data into circular buffer
// @param cbuf circular buffer pointer
// @param data ADC read data
void circular_buf_put(circular_handle_t cbuf, uint16_t data){
cbuf->buffer[cbuf->head] = data;
increase_pointer(cbuf); // Increase pointer when new data update
};
// Digital filter process
// @param cbuf
// @param *filter
// @return float output of filter
double filter_process(circular_handle_t cbuf, double *filter){
int filterOrder = 0;
int bufferIndex = cbuf->head; // Current buffer head index
double output = 0.0f;
for(filterOrder = 0; filterOrder < cbuf->max; filterOrder++){
// Calculate the FIR filter order coefficient and buffer value
output = output + cbuf->buffer[bufferIndex] * filter[filterOrder];
if(bufferIndex == 0) // The coefficient index increase, buffer index decrease
bufferIndex = cbuf->max - 1;
else
bufferIndex--;
}
return output;
};