-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathiir.cpp
More file actions
99 lines (85 loc) · 2.89 KB
/
iir.cpp
File metadata and controls
99 lines (85 loc) · 2.89 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
92
93
94
95
96
97
98
99
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <backend.hpp>
#include <common/err_common.hpp>
#include <convolve.hpp>
#include <handle.hpp>
#include <iir.hpp>
#include <af/arith.h>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <af/signal.h>
#include <cstdio>
using af::dim4;
using detail::cdouble;
using detail::cfloat;
af_err af_fir(af_array* y, const af_array b, const af_array x) {
try {
af_array out;
AF_CHECK(af_convolve1(&out, x, b, AF_CONV_EXPAND, AF_CONV_AUTO));
dim4 xdims = getInfo(x).dims();
af_seq seqs[] = {af_span, af_span, af_span, af_span};
seqs[0].begin = 0.;
seqs[0].end = static_cast<double>(xdims[0]) - 1.;
seqs[0].step = 1.;
af_array res;
AF_CHECK(af_index(&res, out, 4, seqs));
AF_CHECK(af_release_array(out));
std::swap(*y, res);
}
CATCHALL;
return AF_SUCCESS;
}
template<typename T>
inline static af_array iir(const af_array b, const af_array a,
const af_array x) {
return getHandle(iir<T>(getArray<T>(b), getArray<T>(a), getArray<T>(x)));
}
af_err af_iir(af_array* y, const af_array b, const af_array a,
const af_array x) {
try {
const ArrayInfo& ainfo = getInfo(a);
const ArrayInfo& binfo = getInfo(b);
const ArrayInfo& xinfo = getInfo(x);
af_dtype xtype = xinfo.getType();
ARG_ASSERT(1, ainfo.getType() == xtype);
ARG_ASSERT(2, binfo.getType() == xtype);
ARG_ASSERT(1, binfo.ndims() == ainfo.ndims());
dim4 adims = ainfo.dims();
dim4 bdims = binfo.dims();
dim4 xdims = xinfo.dims();
if (xinfo.ndims() == 0) { return af_retain_array(y, x); }
if (xinfo.ndims() > 1) {
if (binfo.ndims() > 1) {
for (int i = 1; i < 3; i++) {
ARG_ASSERT(1, bdims[i] == xdims[i]);
}
}
}
// If only a0 is available, just normalize b and perform fir
if (adims[0] == 1) {
af_array bnorm = 0;
AF_CHECK(af_div(&bnorm, b, a, true));
AF_CHECK(af_fir(y, bnorm, x));
AF_CHECK(af_release_array(bnorm));
return AF_SUCCESS;
}
af_array res;
switch (xtype) {
case f32: res = iir<float>(b, a, x); break;
case f64: res = iir<double>(b, a, x); break;
case c32: res = iir<cfloat>(b, a, x); break;
case c64: res = iir<cdouble>(b, a, x); break;
default: TYPE_ERROR(1, xtype);
}
std::swap(*y, res);
}
CATCHALL;
return AF_SUCCESS;
}