2
Copyright (C) 2010-2011 Nicolas Bourdaud <nicolas.bourdaud@epfl.ch>
4
This file is part of the rtfilter library
6
The rtfilter library is free software: you can redistribute it and/or
7
modify it under the terms of the version 3 of the GNU Lesser General
8
Public License as published by the Free Software Foundation.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU Lesser General Public License for more details.
15
You should have received a copy of the GNU Lesser General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
27
#include "filter-internal.h"
28
#include "rtf_common.h"
33
struct rtf_filter filt;
34
filter_proc lp_filter_fn;
41
#define get_sampler(p) \
42
((struct sampler*)(((char*)p)-offsetof(struct sampler, filt)))
46
unsigned int downsampler_filter(const struct rtf_filter* filt,
47
const void* x, void* y, unsigned int ns)
49
unsigned int i, j, nsproc;
50
struct sampler* sampler = get_sampler(filt);
52
char* out = y, *tmpbuf = sampler->buff;
53
unsigned int samsize = sampler->samsize, r = sampler->r;
54
unsigned int nsret = 0, ci = sampler->ci;
56
// Process data by chunk of BUFFNS samples maximum
58
nsproc = (ns > BUFFNS) ? BUFFNS : ns;
61
sampler->lp_filter_fn(&(sampler->filt), in, tmpbuf, nsproc);
67
memcpy(out+j*samsize, tmpbuf+i*samsize, samsize);
75
out += nsproc*samsize;
76
ci = (ci + nsproc) % r;
85
void downsampler_init_filter(const struct rtf_filter* filt, const void* in)
87
struct sampler* sampler = get_sampler(filt);
89
default_init_filter(filt, in);
95
void downsampler_destroy_filter(const struct rtf_filter* filt)
97
struct sampler* sampler = get_sampler(filt);
99
default_free_filter(filt);
100
align_free(sampler->buff);
108
hfilter rtf_create_downsampler(unsigned int nch, int type, unsigned int r)
110
const struct rtf_filter* lowpass;
111
struct sampler* sampler;
113
double cutoff = 0.8/(double)(2*r);
114
unsigned int samsize = nch*sizeof_data(type);
116
// Allocate resource (+ create lowpass)
117
sampler = malloc(sizeof(*sampler));
118
buff = align_alloc(16, BUFFNS*samsize);
119
lowpass = rtf_create_chebychev(nch, type, cutoff, 8, 0, 0.0005);
120
if (sampler == NULL || buff == NULL || lowpass == NULL) {
123
rtf_destroy_filter(lowpass);
127
// Integrate the lowpass into the sampler structure
128
memcpy(&(sampler->filt), lowpass, sizeof(*lowpass));
129
free((void*) lowpass);
131
// fill sampler structure
132
sampler->lp_filter_fn = sampler->filt.filter_fn;
133
sampler->samsize = samsize;
136
sampler->buff = buff;
138
// Setup virtual methods
139
sampler->filt.filter_fn = downsampler_filter;
140
sampler->filt.destroy_filter_fn = downsampler_destroy_filter;
141
sampler->filt.init_filter_fn = downsampler_init_filter;
143
return &(sampler->filt);