-
Notifications
You must be signed in to change notification settings - Fork 0
/
bandlimsnd.m
124 lines (98 loc) · 3.02 KB
/
bandlimsnd.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
116
117
118
119
120
121
122
123
124
function [snd] = bandlimsnd(snd, fs, lo, hi, ord);
%**
%** "bandlimsnd.m"
%**
%** Kathryn A. Cortopassi, 2001-2004
%**
%** Function to bandlimit a sound waveform or cell array of sound waveforms
%**
%** Syntax: [snd] = bandlimsnd(snd, fs, lo, hi, ord);
%**
%** 'snd' is a single sound waveform or cell array of sound waveforms
%** 'fs' is the sampling rate of the sound waveforms, or a cell array of
%** sampling rates as appropriate (even if 'snd' is a cell array,
%** 'fs' can still be a scalar)
%** 'lo' is the low frequency corner in Hz
%** 'hi' is the high frequency corner in Hz
%** 'ord' is the filter order
%**
%** 'snd' returns the bandlimited sound waveform
%**
%**
%** by Kathryn A. Cortopassi
%** 12-November-2004
%** add 'ord' June 2005
%**
%% set flag to show internal diagnostic plots
plot_internal_diag = 0;
% check for correct number of inputs
argLo = 5; argHi = inf;
error(nargchk(argLo, argHi, nargin));
if ~iscell(fs) & length(fs) == 1
wlo = lo/(fs/2);
whi = hi/(fs/2);
end
if iscell(snd)
for i = 1:length(snd)
%% bandpass filter the sound waveforms based on lo and hi
%% first get the FIR coefficients
%% get coefficients for an order 'ord' bandpass filter
if iscell(fs)
wlo = lo/(fs{i}/2);
whi = hi/(fs{i}/2);
elseif length(fs) > 1
wlo = lo/(fs(i)/2);
whi = hi/(fs(i)/2);
end
if (wlo == 0) & (0 < whi & whi < 1)
%% generate a lowpass filter
b = fir1(ord, whi);
elseif (0 < wlo & wlo < 1) & (whi == 1)
%% generate a highpass filter
b = fir1(ord, wlo, 'high');
elseif (0 < wlo & wlo < 1) & (0 < whi & whi < 1)
%% generate a bandpass filter
b = fir1(ord, [wlo, whi]);
elseif (wlo == 0) & (whi == 1)
%% no filtering needed, drop through loop
continue;
elseif wlo == whi
%% no filtering possible, drop through loop
continue;
end
%% then do zero phase filtering; because of forward and reverse
%% filtering, actual order will be 2*ord
snd{i} = filtfilt(b, 1, snd{i});
if plot_internal_diag
fvtool(b, 1);
end
end
else
%% bandpass filter the sound waveform based on lo and hi
%% first get the FIR coefficients
%% get coefficients for an order 'ord' bandpass filter
if (wlo == 0) & (0 < whi & whi < 1)
%% generate a lowpass filter
b = fir1(ord, whi);
elseif (0 < wlo & wlo < 1) & (whi == 1)
%% generate a highpass filter
b = fir1(ord, wlo, 'high');
elseif (0 < wlo & wlo < 1) & (0 < whi & whi < 1)
%% generate a bandpass filter
b = fir1(ord, [wlo, whi]);
elseif (wlo == 0) & (whi == 1)
%% no filtering needed, drop out
return;
elseif wlo == whi
%% no filterign possible, drop out
return;
end
%% then do zero phase filtering; because of forward and reverse
%% filtering, actual order will be 2*ord
snd = filtfilt(b, 1, snd);
if plot_internal_diag
fvtool(b, 1);
end
end
% end function
return;