Add FIR filter code
This commit is contained in:
		
							
								
								
									
										64
									
								
								stm-firmware/fir-filter.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								stm-firmware/fir-filter.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
/* Reflow Oven Controller
 | 
			
		||||
*
 | 
			
		||||
* Copyright (C) 2021  Mario Hüttel <mario.huettel@gmx.net>
 | 
			
		||||
*
 | 
			
		||||
* This file is part of the Reflow Oven Controller Project.
 | 
			
		||||
*
 | 
			
		||||
* The reflow oven controller is free software: you can redistribute it and/or modify
 | 
			
		||||
* it under the terms of the GNU General Public License version 2 as
 | 
			
		||||
* published by the Free Software Foundation.
 | 
			
		||||
*
 | 
			
		||||
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
 | 
			
		||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
			
		||||
* GNU General Public License for more details.
 | 
			
		||||
*
 | 
			
		||||
* You should have received a copy of the GNU General Public License
 | 
			
		||||
* along with the reflow oven controller project.
 | 
			
		||||
* If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <reflow-controller/fir-filter.h>
 | 
			
		||||
 | 
			
		||||
void fir_filter_init_null(struct fir_filter_fixed *filter)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t idx;
 | 
			
		||||
 | 
			
		||||
	if (!filter)
 | 
			
		||||
		return;
 | 
			
		||||
	filter->put_idx = 0U;
 | 
			
		||||
	for (idx = 0; idx < filter->tap_count; idx++)
 | 
			
		||||
		filter->data = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int32_t fir_filter_process_sample(struct fir_filter_fixed *filter, int32_t sample)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t tap_idx;
 | 
			
		||||
	uint32_t coeff_idx;
 | 
			
		||||
	int32_t output = 0;
 | 
			
		||||
 | 
			
		||||
	if (!filter)
 | 
			
		||||
		return 0;
 | 
			
		||||
 | 
			
		||||
	/* Store the new data point */
 | 
			
		||||
	filter->data[filter->put_idx] = sample;
 | 
			
		||||
 | 
			
		||||
	coeff_idx = filter->put_idx;
 | 
			
		||||
 | 
			
		||||
	/* Apply fir taps on data */
 | 
			
		||||
	for (tap_idx = 0; tap_idx < filter->tap_count; tap_idx++) {
 | 
			
		||||
		output += filter->data[tap_idx] * filter->coefficients[coeff_idx];
 | 
			
		||||
 | 
			
		||||
		if (coeff_idx)
 | 
			
		||||
			coeff_idx--;
 | 
			
		||||
		else
 | 
			
		||||
			coeff_idx = filter->tap_count-1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	filter->put_idx++;
 | 
			
		||||
	if (filter->put_idx >= filter->tap_count)
 | 
			
		||||
		filter->put_idx = 0;
 | 
			
		||||
 | 
			
		||||
	return output;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								stm-firmware/include/reflow-controller/fir-filter.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								stm-firmware/include/reflow-controller/fir-filter.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
/* Reflow Oven Controller
 | 
			
		||||
*
 | 
			
		||||
* Copyright (C) 2021  Mario Hüttel <mario.huettel@gmx.net>
 | 
			
		||||
*
 | 
			
		||||
* This file is part of the Reflow Oven Controller Project.
 | 
			
		||||
*
 | 
			
		||||
* The reflow oven controller is free software: you can redistribute it and/or modify
 | 
			
		||||
* it under the terms of the GNU General Public License version 2 as
 | 
			
		||||
* published by the Free Software Foundation.
 | 
			
		||||
*
 | 
			
		||||
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
 | 
			
		||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
			
		||||
* GNU General Public License for more details.
 | 
			
		||||
*
 | 
			
		||||
* You should have received a copy of the GNU General Public License
 | 
			
		||||
* along with the reflow oven controller project.
 | 
			
		||||
* If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#ifndef __FIR_FILTER_H__
 | 
			
		||||
#define __FIR_FILTER_H__
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
struct fir_filter_fixed {
 | 
			
		||||
    uint32_t tap_count;
 | 
			
		||||
    const int32_t *coefficients;
 | 
			
		||||
    int32_t *data;
 | 
			
		||||
    uint32_t put_idx;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void fir_filter_init_null(struct fir_filter_fixed *filter);
 | 
			
		||||
 | 
			
		||||
int32_t fir_filter_process_sample(struct fir_filter_fixed *filter, int32_t sample);
 | 
			
		||||
 | 
			
		||||
#endif /* __FIR_FILTER_H__ */
 | 
			
		||||
		Reference in New Issue
	
	Block a user