-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputfilterParameters.sci
More file actions
109 lines (88 loc) · 3.74 KB
/
inputfilterParameters.sci
File metadata and controls
109 lines (88 loc) · 3.74 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
loadXcosLibs;
// Define a function that generates filter blocks with user-defined parameters
function [x, y, typ] = FilterBlock(job, arg1, arg2, filter_type)
x = [];
y = [];
typ = [];
select job
case 'set' then
x = arg1;
// Display dialog for setting parameters
params = x_dialog("Set Filter Parameters", ..
list("Sampling Frequency (Hz)", "Cutoff Frequency (Hz)", "Upper Cutoff (for Band filters)"), ..
list("100", "10", "15"));
// Updating the model parameters based on user input
if ~isempty(params) then
x.model.ipar = [evstr(params(1)), evstr(params(2)), evstr(params(3)), filter_type];
end
case 'define' then
// Set up the model
model = scicos_model();
model.sim = list('filter_block', 4);
model.blocktype = 'c';
model.in = [1];
model.out = [1];
model.ipar = [100, 10, 15, filter_type]; // Default values for Sampling frequency, Cutoff frequencies, and Filter type
model.rpar = [];
model.evtin = [];
model.evtout = [];
model.pout = 2;
model.dstate = [];
x = standard_define([2 2], model, [], []);
// Setting the block name based on filter type
select filter_type
case 1 then x.model.orig = "Low Pass Filter";
case 2 then x.model.orig = "High Pass Filter";
case 3 then x.model.orig = "Band Pass Filter";
case 4 then x.model.orig = "Band Stop Filter";
end
end
function [y] = filter_block(t, u, model, in, out)
fs = model.ipar(1); // Sampling frequency
fc1 = model.ipar(2); // Cutoff or lower cutoff frequency
fc2 = model.ipar(3); // Upper cutoff frequency (for band filters)
filter_type = model.ipar(4); // Type of filter
select filter_type
case 1 then // Low-pass filter
[b, a] = butter(2, fc1 / (fs / 2), 'low');
case 2 then // High-pass filter
[b, a] = butter(2, fc1 / (fs / 2), 'high');
case 3 then // Band-pass filter
[b, a] = butter(2, [fc1 fc2] / (fs / 2), 'bandpass');
case 4 then // Band-stop filter
[b, a] = butter(2, [fc1 fc2] / (fs / 2), 'stop');
end
y(1) = filter(b, a, u);
endfunction
endfunction
// Define functions for each filter type, setting filter_type value (1 = Low-pass, 2 = High-pass, 3 = Band-pass, 4 = Band-stop)
function [x, y, typ] = LowPassFilter(job)
[x, y, typ] = FilterBlock(job, [], [], 1);
endfunction
function [x, y, typ] = HighPassFilter(job)
[x, y, typ] = FilterBlock(job, [], [], 2);
endfunction
function [x, y, typ] = BandPassFilter(job)
[x, y, typ] = FilterBlock(job, [], [], 3);
endfunction
function [x, y, typ] = BandStopFilter(job)
[x, y, typ] = FilterBlock(job, [], [], 4);
endfunction
style = struct();
style.fillColor = "red";
block_img = SCI + "/modules/xcos/images/blocks/RAMP.svg";
if getos() == "Windows" then
block_img = "/" + block_img;
end
style.image = "file://" + block_img;
lowPass = LowPassFilter('define');
highPass = HighPassFilter('define');
bandPass = BandPassFilter('define');
bandStop = BandStopFilter('define');
// Create a new palette and add each filter block with proper names
pal = xcosPal("My Filters");
pal = xcosPalAddBlock(pal, lowPass, SCI + "/modules/xcos/images/palettes/RAMP.png", style);
pal = xcosPalAddBlock(pal, highPass, SCI + "/modules/xcos/images/palettes/RAMP.png", style);
pal = xcosPalAddBlock(pal, bandPass, SCI + "/modules/xcos/images/palettes/RAMP.png", style);
pal = xcosPalAddBlock(pal, bandStop, SCI + "/modules/xcos/images/palettes/RAMP.png", style);
xcosPalAdd(pal);