-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIR.m
100 lines (84 loc) · 2.71 KB
/
FIR.m
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
100
% FILE: FIR.m
%
% FUNCTION: FIR
%
% CALL: [b, a] = FIR(order, wc, Ts, window, type)
%
% The functions returns a FIR filter of type type and of order order and cutoff
% frequency wc with sampling period Ts using the window given by the vector window
%
% INPUTS:
% order - filter order
% wc - cutoff frequency
% Ts - sampling period
% window - a vector with the window elements, the vector has size
% order + 1
% type - low or high, depending on what filter you want to create
%
% OUTPUTS:
% b - filter denominator coefficients
% a - filter numerator coefficients
% Author: Leonard-Gabriel Necula
% Created: December 24 2020
% Updated: January 18 2021
function [b, a] = FIR(order, wc, Ts, window, type)
if nargin < 1
order = 16;
disp('Order was not set. Set order to 16');
elseif isempty(order)
order = 16;
disp('Order was left empty. Set order to 16');
end
if nargin < 2
wc = 0.4 * pi;
disp('Cutoff frequency was not set. Set wc to 0.4pi');
elseif isempty(wc)
wc = 0.4 * pi;
disp('Cutoff frequency was left empty. Set wc to 0.4pi');
end
if nargin < 3
disp('The sampling period was not set. Ts set to 1');
Ts = 1;
elseif isempty(Ts)
disp('The sampling period was left empty. Ts set to 1');
Ts = 1;
end
if nargin < 4
window = hamming(order + 1);
disp('Window was not set. Default window used');
elseif isempty(window)
window = hamming(order + 1);
disp('Window was left empty. Default window used');
elseif numel(window) ~= order + 1
window = hamming(order + 1);
disp('Window of incorrect size. Default window used');
end
if nargin < 5
disp('Missing type, default type = low');
type = 'low';
elseif isempty(type)
disp('Empty type, default type = low');
type = 'low';
end
switch type
case 'low'
hn = cardinalSinShifted(order + 1, wc, Ts);
hn = hn(:);
b = hn .* window(:);
% Norming
if sum(b) > 1e-10
b = b./sum(b);
end
case 'high'
hn = cardinalSinShifted(order + 1, pi - wc, Ts);
hn = hn(:);
b = hn .* window(:);
% Norming
if sum(b) > 1e-10
b = b./sum(b);
end
b(2:2:end) = -b(2:2:end);
end
% b = hn .* window(:);
a = 1;
end