-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_scratch.m
More file actions
executable file
·110 lines (86 loc) · 2.83 KB
/
Copy pathfilter_scratch.m
File metadata and controls
executable file
·110 lines (86 loc) · 2.83 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
100
101
102
103
104
105
106
107
108
109
110
clear all
close all
%%%%% Input parameters %%%%%
% where to find comma seperated data
ptf = '/Users/Orenstein/Desktop/corrected_counts_091817.txt';
data = dlmread(ptf, ',');
% get first and last nonzero indicies
nz_front = find(data(:, 2), 1, 'first');
nz_end = find(data(:, 2), 1, 'last');
% pull out the relevent data
data = data(nz_front:nz_end, :); % hard coded to truncate zeros
raw.oith = data(1:end, 2) - data(1:end, 3) + data(1:end, 4);
raw.para = data(1:end, 8) - data(1:end, 9) + data(1:end, 10);
raw.egg = data(1:end, 5) - data(1:end, 6) + data(1:end, 7);
% names
names = {'oith', 'para', 'egg'};
%%%%% Date and time labels for plotting %%%%%
startDate = datenum('03-11-2015 12:00:00 PM');
endDate = datenum('08-01-2015 12:00:00 AM');
xx = linspace(startDate, endDate, max(size(data)));
str = datestr(xx, 'mm-dd');
%%
%%%%% Power Spectra %%%%%
% sampling frequency of 1 hour
for ii = 1:3
in_ser = raw.(names{ii});
[pxx, freq] = pwelch(in_ser-mean(in_ser), 1024, [], [], 1);
figure;
semilogx(freq, 10*log10(pxx))
title(['Welch PSD ', names{ii}, ' win = 128, 50% overlap'], 'FontSize', 14)
xlabel('Frequency (1/hours)', 'FontSize', 12)
ylabel('Power', 'FontSize', 12)
set(gca, 'FontSize', 12)
grid on
end
%%
%%%%% Low pass filter %%%%%
f_cut = 1/(12*24); % cut off frequency as 1/# hours
% FIR filter with hamming window
d = designfilt('lowpassfir', 'FilterOrder', 10, ...
'CutoffFrequency', f_cut, 'DesignMethod', 'window');
% FIR filter with equiripple
% d = designfilt('lowpassfir', ...
% 'CutoffFrequency', f_cut, 'DesignMethod', 'equiripple');
f_delay = (length(d.Coefficients)-1)/2; % compute phase delay
% filter the data
filt.oith = filter(d, raw.oith);
filt.para = filter(d, raw.para);
filt.egg = filter(d, raw.egg);
% plot it
figure; plot(xx - f_delay/24, filt.oith, xx, raw.oith)
title('oith')
figure; plot(xx - f_delay/24, filt.para, xx, raw.para)
title('para')
figure; plot(xx - f_delay/24, filt.egg, xx, raw.egg)
title('egg')
%%
%%%%% Notch parameters %%%%%
f0 = 1/24; % notch frequency 1
f1 = 1/12; % notch frequency 2
fn = 1/2; % nyquist frequency assuming sampling of 1 hour
f0n = f0/fn; % normalize the frequency for the notch
f1n = f1/fn;
n_width = 0.001; % notch width
%%
%%%%% Butterworth band stop %%%%%
wg1 = [f0n - n_width, f0n + n_width];
[b1, a1] = butter(2, wg1, 'stop');
wg2 = [f1n - n_width, f1n + n_width];
[b2, a2] = butter(2, wg2, 'stop');
a0 = conv(a1, a2);
b0 = conv(b1, b2);
test = filter(b0, a0, raw.oith);
[pxx, freq] = pwelch(test - mean(test), 128, [], [], 1);
figure; semilogx(freq, 10*log10(pxx))
grid on
title('Butterworth band stop')
%%
%%%%% Notch using fdesign %%%%%
dd = fdesign.notch(8, f0n, 10);
Hd = design(dd, 'Systemobject', true);
test = Hd(raw.oith);
[pxx, freq] = pwelch(test - mean(test), 128, [], [], 1);
figure; semilogx(freq, 10*log10(pxx))
grid on
title('fdesign notch')