~ubuntu-branches/ubuntu/vivid/rtfilter/vivid

« back to all changes in this revision

Viewing changes to src/downsampler.c

  • Committer: Package Import Robot
  • Author(s): Nicolas Bourdaud
  • Date: 2011-12-01 12:09:30 UTC
  • Revision ID: package-import@ubuntu.com-20111201120930-lmia8ytlwmif9yta
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2010-2011 Nicolas Bourdaud <nicolas.bourdaud@epfl.ch>
 
3
 
 
4
    This file is part of the rtfilter library
 
5
 
 
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.
 
9
  
 
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.
 
14
    
 
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/>.
 
17
*/
 
18
#if HAVE_CONFIG_H
 
19
# include <config.h>
 
20
#endif
 
21
 
 
22
#include <stddef.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
 
 
26
#include "rtfilter.h"
 
27
#include "filter-internal.h"
 
28
#include "rtf_common.h"
 
29
 
 
30
#define BUFFNS  64
 
31
 
 
32
struct sampler {
 
33
        struct rtf_filter filt;
 
34
        filter_proc lp_filter_fn;
 
35
        unsigned int r;
 
36
        unsigned int ci;
 
37
        unsigned int samsize;
 
38
        void* buff;
 
39
};
 
40
 
 
41
#define get_sampler(p) \
 
42
        ((struct sampler*)(((char*)p)-offsetof(struct sampler, filt)))
 
43
 
 
44
 
 
45
static
 
46
unsigned int downsampler_filter(const struct rtf_filter* filt, 
 
47
                                const void* x, void* y, unsigned int ns)
 
48
{
 
49
        unsigned int i, j, nsproc;
 
50
        struct sampler* sampler = get_sampler(filt);
 
51
        const char* in = x;
 
52
        char* out = y, *tmpbuf = sampler->buff;
 
53
        unsigned int samsize = sampler->samsize, r = sampler->r;
 
54
        unsigned int nsret = 0, ci = sampler->ci;
 
55
 
 
56
        // Process data by chunk of BUFFNS samples maximum
 
57
        while (ns) {
 
58
                nsproc = (ns > BUFFNS) ? BUFFNS : ns;
 
59
 
 
60
                // Apply lowpass
 
61
                sampler->lp_filter_fn(&(sampler->filt), in, tmpbuf, nsproc);
 
62
 
 
63
                // Apply decimation
 
64
                i = r-1 - ci;
 
65
                j = 0;
 
66
                while (i < ns) {
 
67
                        memcpy(out+j*samsize, tmpbuf+i*samsize, samsize);
 
68
                        j++;
 
69
                        i += r;
 
70
                }
 
71
 
 
72
                nsret += j;
 
73
                ns -= nsproc;
 
74
                in += nsproc*samsize;
 
75
                out += nsproc*samsize;
 
76
                ci = (ci + nsproc) % r;
 
77
        }
 
78
 
 
79
        sampler->ci = ci;
 
80
        return nsret;
 
81
}
 
82
 
 
83
 
 
84
static
 
85
void downsampler_init_filter(const struct rtf_filter* filt, const void* in)
 
86
{
 
87
        struct sampler* sampler = get_sampler(filt);
 
88
 
 
89
        default_init_filter(filt, in);
 
90
        sampler->ci = 0;
 
91
}
 
92
 
 
93
 
 
94
static
 
95
void downsampler_destroy_filter(const struct rtf_filter* filt)
 
96
{
 
97
        struct sampler* sampler = get_sampler(filt);
 
98
 
 
99
        default_free_filter(filt);
 
100
        align_free(sampler->buff);
 
101
 
 
102
        free(sampler);
 
103
}
 
104
 
 
105
 
 
106
 
 
107
API_EXPORTED
 
108
hfilter rtf_create_downsampler(unsigned int nch, int type, unsigned int r)
 
109
{
 
110
        const struct rtf_filter* lowpass;
 
111
        struct sampler* sampler;
 
112
        void* buff;
 
113
        double cutoff = 0.8/(double)(2*r);
 
114
        unsigned int samsize = nch*sizeof_data(type);
 
115
        
 
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) {
 
121
                free(sampler);
 
122
                align_free(buff);
 
123
                rtf_destroy_filter(lowpass);
 
124
                return NULL;
 
125
        }
 
126
 
 
127
        // Integrate the lowpass into the sampler structure
 
128
        memcpy(&(sampler->filt), lowpass, sizeof(*lowpass));
 
129
        free((void*) lowpass);
 
130
 
 
131
        // fill sampler structure
 
132
        sampler->lp_filter_fn = sampler->filt.filter_fn;
 
133
        sampler->samsize = samsize;
 
134
        sampler->r = r;
 
135
        sampler->ci = 0;
 
136
        sampler->buff = buff;
 
137
 
 
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;
 
142
 
 
143
        return &(sampler->filt);
 
144
}
 
145