-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilter.c
More file actions
28 lines (23 loc) · 1018 Bytes
/
filter.c
File metadata and controls
28 lines (23 loc) · 1018 Bytes
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
#include "filter.h"
#include <stdio.h>
#include <math.h>
// Low-pass filter function with alpha as a parameter
float low_pass_filter(float input, float *previous_output, float alpha) {
// Apply the low-pass filter formula
float output = alpha * input + (1 - alpha) * (*previous_output);
// Update previous output
*previous_output = output;
return output;
}
float low_pass_filter_time(float input, float *previous_output, float alpha, float delta_time) {
float effective_alpha = delta_time / (delta_time + alpha);
float output = effective_alpha * input + (1 - effective_alpha) * (*previous_output);
*previous_output = output;
return output;
}
float calculate_alpha(float cutoff_frequency, float sampling_frequency) {
return cutoff_frequency / (cutoff_frequency + sampling_frequency);
}
double calculate_delta_time(struct timespec *start, struct timespec *end) {
return (end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec) / 1e9;
}