summaryrefslogtreecommitdiff
path: root/macros/decimate.sci
blob: 9f8aeb54dea2168570a2c48f8b0361cd57dd33b3 (plain)
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
function y = decimate(x, q, n, ftype)
//Decimation — decrease sample rate by integer factor

//Calling Sequence
//y = decimate(x,q)
//y = decimate(x,q,n)
// y = decimate (…, "fir")

//Parameters
//x: input sequence
//q: reduction factor
//n : filter order
//ftype: filter type : iir or fir

//Description
//this is an octave function
//y = decimate(x,q) reduces the sample rate of x, the input signal, by a factor of q.
//By default, an order n Chebyshev type I filter is used. If n is not specified, the default is 8.
//If the optional argument "fir" is given, an order n FIR filter is used, with a default order of 30 if n is not given.
//Note that q must be an integer for this rate change method.
//
//Example :
//t = 0:.00025:1;
//x = sin(2*%pi*30*t) + sin(2*%pi*60*t);
//y = decimate(x,4);
//subplot(211);
//plot2d3((0:120),x(1:121));
//subplot(212);
//plot2d3((0:30),y(1:31));

//This will result in plots of original sequence v/s sample number and decimated sequence v/s sample number

rhs = argn(2)
if(rhs<2 | rhs>4)
error("Wrong number of input arguments.")
elseif(~(sum(length(q)==1) & q == fix (q) & q > 0))
error("Parameter 2 must be a positive integer.")
end
//if (nargin < 3)
if(argn(2) < 3)
ftype = "iir"
n = []
//elseif (nargin < 4)
elseif(argn(2) < 4)
if (ischar (n))
ftype = n
n = []
else
ftype = "iir"
end
end

//if (~ and(strcmp (ftype, {"fir", "iir"})))    // if strings are equal strcmp returns 0
if(strcmp(ftype,"iir") & strcmp(ftype,"fir"))
error("Filter type must be either fir or iir.")
end

fir = strcmp (ftype, "fir")
if (isempty (n))
if (fir)
n = 30
else
n = 8
end
end

if(~(sum(length(n)==1) & n == fix (n) & n > 0))
error("N must be a positive integer.")
end
select(rhs)
case 2 then
y = callOctave("decimate", x, q)
case 3 then
y = callOctave("decimate", x, q, n)
case 4 then
y = callOctave("decimate", x, q, n, ftype)
end
endfunction