-
Notifications
You must be signed in to change notification settings - Fork 2
/
cornerFreq.m
116 lines (107 loc) · 2.96 KB
/
cornerFreq.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
% corner frequency of filter, either FIR, polynomial, or savitzky-golay
% returns corner frequeny, Fc, and [f, r], frequency in hz and response.
% Inputs:
% Fs is sampling rate (e.g., 1000 Hz)
% length(varargin) = 1 ==> FIR filter
% = 2 ==> if scalar, Savitzky Golay, [k, F]
% if vector, polynomial
% LMO 25mar2015 create
function [Fc, Freqs, Resp] = cornerFreq(Fs, varargin)
debug = 0;
twopi = 2 * pi;
nFFT = 5 * 1024;
Fn = Fs/2; % nyquist freq
F0 = Fn/ nFFT;
Fhi = 200; % Hz
dF = 0.25; % hz
Freqs = F0:dF:Fhi;
nFreqs = length(Freqs);
% create time
dt = 1/Fs;
t = 0:dt:1;
figure(1)
clf
Resp = 0;
switch(numel(varargin))
case 1
% FIR filter
filt = varargin{1};
for k = 1:nFreqs
y = sin(twopi * Freqs(k) * t);
if numel(filt) > numel(y)
a = filt;
b = y;
else
a = y;
b = filt;
end
z = conv(a, b);
lo = fix(length(z)/2 - 0.35/dt);
hi = fix(lo + 0.7/dt);
z = z(lo:hi);
Resp(k) = max(abs(z));
if debug
tp = ((1:length(z))-1)*dt;
plot(tp, z);
ylim([-1, 1]);
disp(Freqs(k))
pause(1)
end
end
case 2
a = varargin{1};
b = varargin{2};
if length(a) == 1 % scalar, so is savitzky-golay(x, a,b)
for k = 1:1:nFreqs
y = sin(twopi * Freqs(k) * t);
z = sgolayfilt(y, a, b);
lo = fix(length(z)/2 - 0.25/dt);
hi = fix(lo + 0.5/dt);
z = z(lo:hi);
Resp(k) = max(abs(z));
if debug
tp = ((1:length(z))-1)*dt;
plot(tp, z);
ylim([-1, 1]);
disp(Freqs(k))
pause(1)
end
end
else
polyDenom = a;
polyNumer = b;
for k = 1:1:nFreqs
y = sin(twopi * Freqs(k) * t);
z = filter(polyNumer, polyDenom, y);
lo = fix(length(z)/2 - 0.25/dt);
hi = fix(lo + 0.5/dt);
z = z(lo:hi);
Resp(k) = max(abs(z));
if debug
tp = ((1:length(z))-1)*dt;
plot(tp, z);
ylim([-1, 1]);
disp(Freqs(k))
pause(1)
end
end
end
end
[mxResp, khi] = max(Resp);
Resp = Resp/mxResp;
plot(Freqs, Resp);
rng = khi : length(Resp);
k = find(Resp(rng) > 1/sqrt(2));
k = rng(k(end));
if length(k) == 1
Fc = Freqs(k);
else
Fc = (Freqs(k) + Freqs(k+1))/2;
end
hold on
y0 = Resp(k);
plot(Fc * [1, 1], y0 * [0,1], 'r--');
plot(Fc * [0, 1], y0 * [1, 1], 'r--');
ht = text(Fc * 1.1, y0, sprintf('Fc = %3.2f Hz', Fc));
xlabel('Freq (Hz)');
ylabel('Gain');