0001 function varargout = pzmodel_helper(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 gui_Singleton = 1;
0033 gui_State = struct('gui_Name', mfilename, ...
0034 'gui_Singleton', gui_Singleton, ...
0035 'gui_OpeningFcn', @pzmodel_helper_OpeningFcn, ...
0036 'gui_OutputFcn', @pzmodel_helper_OutputFcn, ...
0037 'gui_LayoutFcn', [] , ...
0038 'gui_Callback', []);
0039 if nargin && ischar(varargin{1})
0040 gui_State.gui_Callback = str2func(varargin{1});
0041 end
0042
0043 if nargout
0044 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0045 else
0046 gui_mainfcn(gui_State, varargin{:});
0047 end
0048
0049
0050
0051
0052 function pzmodel_helper_OpeningFcn(hObject, eventdata, handles, varargin)
0053
0054
0055
0056
0057
0058
0059
0060 handles.output = hObject;
0061
0062
0063 guidata(hObject, handles);
0064
0065
0066
0067
0068
0069 poles = [];
0070 zeros = [];
0071
0072 setappdata(handles.main, 'poles', poles);
0073 setappdata(handles.main, 'zeros', zeros);
0074 setappdata(handles.main, 'filt', []);
0075
0076
0077 function varargout = pzmodel_helper_OutputFcn(hObject, eventdata, handles)
0078
0079
0080
0081
0082
0083
0084 varargout{1} = handles.output;
0085
0086
0087
0088 function pzEdit_Callback(hObject, eventdata, handles)
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 function pzEdit_CreateFcn(hObject, eventdata, handles)
0099
0100
0101
0102
0103
0104
0105 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0106 set(hObject,'BackgroundColor','white');
0107 end
0108
0109
0110
0111 function addPoleBtn_Callback(hObject, eventdata, handles)
0112
0113
0114
0115
0116
0117 strVal = get(handles.pzEdit, 'String');
0118 val = str2num(strVal);
0119 f = val(1);
0120 if length(val) == 2
0121 q = val(2);
0122 p = pz(f,q);
0123 else
0124 p = pz(f);
0125 end
0126
0127 poles = getappdata(handles.main, 'poles');
0128 poles = [poles p];
0129 setappdata(handles.main, 'poles', poles);
0130
0131
0132 updatePolesList(handles);
0133
0134
0135 updateModelResp(handles);
0136
0137
0138
0139 function poleList_Callback(hObject, eventdata, handles)
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 function poleList_CreateFcn(hObject, eventdata, handles)
0150
0151
0152
0153
0154
0155
0156 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0157 set(hObject,'BackgroundColor','white');
0158 end
0159
0160
0161
0162 function addZeroBtn_Callback(hObject, eventdata, handles)
0163
0164
0165
0166
0167
0168 strVal = get(handles.pzEdit, 'String');
0169 val = str2num(strVal);
0170 f = val(1);
0171 if length(val) == 2
0172 q = val(2);
0173 z = pz(f,q);
0174 else
0175 z = pz(f);
0176 end
0177
0178 zeros = getappdata(handles.main, 'zeros');
0179 zeros = [zeros z];
0180 setappdata(handles.main, 'zeros', zeros);
0181
0182
0183 updateZerosList(handles);
0184
0185
0186 updateModelResp(handles);
0187
0188
0189
0190 function zeroList_Callback(hObject, eventdata, handles)
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 function zeroList_CreateFcn(hObject, eventdata, handles)
0201
0202
0203
0204
0205
0206
0207 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0208 set(hObject,'BackgroundColor','white');
0209 end
0210
0211
0212 function updatePolesList(handles)
0213
0214 poles = getappdata(handles.main, 'poles');
0215 pstr = [];
0216 for n=1:length(poles)
0217 p = poles(n);
0218 f = p.f;
0219 q = p.q;
0220 if q>0.5
0221 pstr = strvcat(pstr, sprintf('%2.2f Hz Q=%2.2f', f, q));
0222 else
0223 pstr = strvcat(pstr, sprintf('%2.2f Hz', f));
0224 end
0225 end
0226
0227 if isempty(poles)
0228 set(handles.poleList, 'String', ' ');
0229 else
0230 set(handles.poleList, 'String', pstr);
0231 end
0232 set(handles.poleList, 'Value', 1);
0233
0234
0235 function updateZerosList(handles)
0236
0237 zeros = getappdata(handles.main, 'zeros');
0238 pstr = [];
0239 for n=1:length(zeros)
0240 p = zeros(n);
0241 f = p.f;
0242 q = p.q;
0243 if q>0.5
0244 pstr = strvcat(pstr, sprintf('%2.2f Hz Q=%2.2f', f, q));
0245 else
0246 pstr = strvcat(pstr, sprintf('%2.2f Hz', f));
0247 end
0248 end
0249
0250 if isempty(zeros)
0251 set(handles.zeroList, 'String', ' ');
0252 else
0253 set(handles.zeroList, 'String', pstr);
0254 end
0255 set(handles.zeroList, 'Value', 1);
0256
0257
0258 function updateModelResp(handles)
0259
0260 gain = str2double(get(handles.gainEdit, 'String'));
0261 poles = getappdata(handles.main, 'poles');
0262 zeros = getappdata(handles.main, 'zeros');
0263
0264 pzm = pzmodel(gain, poles, zeros);
0265
0266 f1s = get(handles.f1Edit, 'String');
0267 f2s = get(handles.f2Edit, 'String');
0268 nfs = get(handles.nfEdit, 'String');
0269 fss = get(handles.fsEdit, 'String');
0270
0271 if isempty(f1s)
0272 f1 = getlowerFreq(pzm)/10;
0273 else
0274 f1 = str2double(f1s);
0275 end
0276 if isempty(f2s)
0277 f2 = getupperFreq(pzm)*10;
0278 else
0279 f2 = str2double(f2s);
0280 end
0281 if isempty(nfs)
0282 nf = 1000;
0283 else
0284 nf = str2double(nfs);
0285 end
0286
0287 if ~isempty(pzm.poles) || ~isempty(pzm.zeros)
0288
0289
0290 a = resp(pzm, f1, f2, nf);
0291 d = a.data;
0292 mx = d.x;
0293 my = d.y;
0294
0295
0296 if isempty(fss)
0297 filt = miir(plist([param('pzmodel', pzm)]));
0298 else
0299 filt = miir(plist([param('pzmodel', pzm) param('fs', str2double(fss))]));
0300 end
0301 setappdata(handles.main, 'filt', filt);
0302 set(handles.filterStrEdit, 'String', string(filt));
0303
0304 filtresp = resp(filt, plist(param('f', mx.')));
0305 d = filtresp.data;
0306 fx = d.x;
0307 fy = d.y;
0308
0309 axes(handles.magAxes)
0310 loglog(mx,abs(my), fx, abs(fy), 'r--')
0311 grid on;
0312 axis tight
0313 xlabel('');
0314 ylabel('Magnitude');
0315 legend('Pole/Zero model', 'IIR','Location','SouthWest')
0316
0317 axes(handles.phaseAxes)
0318 semilogx(mx, utils.math.phase(my), fx, utils.math.phase(fy), 'r--')
0319 grid on;
0320 axis tight
0321 xlabel('Frequency [Hz]');
0322 ylabel('Phase [deg]');
0323 else
0324 axes(handles.magAxes)
0325 cla
0326 axes(handles.phaseAxes)
0327 cla
0328 end
0329
0330
0331
0332
0333 function gainEdit_Callback(hObject, eventdata, handles)
0334
0335
0336
0337
0338
0339
0340
0341 updateModelResp(handles)
0342
0343
0344 function gainEdit_CreateFcn(hObject, eventdata, handles)
0345
0346
0347
0348
0349
0350
0351 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0352 set(hObject,'BackgroundColor','white');
0353 end
0354
0355
0356
0357 function f1Edit_Callback(hObject, eventdata, handles)
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367 function f1Edit_CreateFcn(hObject, eventdata, handles)
0368
0369
0370
0371
0372
0373
0374 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0375 set(hObject,'BackgroundColor','white');
0376 end
0377
0378
0379
0380 function f2Edit_Callback(hObject, eventdata, handles)
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390 function f2Edit_CreateFcn(hObject, eventdata, handles)
0391
0392
0393
0394
0395
0396
0397 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0398 set(hObject,'BackgroundColor','white');
0399 end
0400
0401
0402
0403 function nfEdit_Callback(hObject, eventdata, handles)
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413 function nfEdit_CreateFcn(hObject, eventdata, handles)
0414
0415
0416
0417
0418
0419
0420 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0421 set(hObject,'BackgroundColor','white');
0422 end
0423
0424
0425
0426 function replotBtn_Callback(hObject, eventdata, handles)
0427
0428
0429
0430
0431 updateModelResp(handles)
0432
0433
0434
0435 function fsEdit_Callback(hObject, eventdata, handles)
0436
0437
0438
0439
0440
0441
0442 updateModelResp(handles)
0443
0444
0445
0446 function fsEdit_CreateFcn(hObject, eventdata, handles)
0447
0448
0449
0450
0451
0452
0453 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0454 set(hObject,'BackgroundColor','white');
0455 end
0456
0457
0458
0459 function figureBtn_Callback(hObject, eventdata, handles)
0460
0461
0462
0463
0464 gain = str2double(get(handles.gainEdit, 'String'));
0465 poles = getappdata(handles.main, 'poles');
0466 zeros = getappdata(handles.main, 'zeros');
0467
0468 pzm = pzmodel(gain, poles, zeros);
0469
0470 f1s = get(handles.f1Edit, 'String');
0471 f2s = get(handles.f2Edit, 'String');
0472 nfs = get(handles.nfEdit, 'String');
0473 fss = get(handles.fsEdit, 'String');
0474
0475 if isempty(f1s)
0476 f1 = getlowerFreq(pzm)/10;
0477 else
0478 f1 = str2double(f1s);
0479 end
0480 if isempty(f2s)
0481 f2 = getupperFreq(pzm)*10;
0482 else
0483 f2 = str2double(f2s);
0484 end
0485 if isempty(nfs)
0486 nf = 1000;
0487 else
0488 nf = str2double(nfs);
0489 end
0490
0491
0492 a = resp(pzm, f1, f2, nf);
0493 a.setName('PZmodel');
0494
0495
0496 if isempty(fss)
0497 filt = miir(plist([param('pzmodel', pzm)]));
0498 else
0499 filt = miir(plist([param('pzmodel', pzm) param('fs', str2double(fss))]));
0500 end
0501
0502
0503 filtresp = resp(filt, plist(param('f', a.data.getX)));
0504 filtresp.setName('IIR filter');
0505
0506 iplot(a, filtresp)
0507
0508
0509
0510 function filterStrEdit_Callback(hObject, eventdata, handles)
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520 function filterStrEdit_CreateFcn(hObject, eventdata, handles)
0521
0522
0523
0524
0525
0526
0527 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0528 set(hObject,'BackgroundColor','white');
0529 end
0530
0531
0532
0533 function clearBtn_Callback(hObject, eventdata, handles)
0534
0535
0536
0537
0538 setappdata(handles.main, 'poles', []);
0539 setappdata(handles.main, 'zeros', []);
0540
0541
0542 updateZerosList(handles);
0543 updatePolesList(handles);
0544
0545
0546 updateModelResp(handles);
0547
0548
0549
0550 function deletePolesBtn_Callback(hObject, eventdata, handles)
0551
0552
0553
0554
0555
0556 values = get(handles.poleList, 'Value');
0557
0558
0559 poles = getappdata(handles.main, 'poles');
0560
0561 po = [];
0562 for j=1:length(poles)
0563 if j~=values
0564 po = [po poles(j)];
0565 end
0566 end
0567
0568 setappdata(handles.main, 'poles', po);
0569 updatePolesList(handles);
0570
0571
0572 updateModelResp(handles);
0573
0574
0575
0576 function deleteZerosBtn_Callback(hObject, eventdata, handles)
0577
0578
0579
0580
0581
0582 values = get(handles.zeroList, 'Value');
0583
0584 zeros = getappdata(handles.main, 'zeros');
0585 zo = [];
0586 for j=1:length(zeros)
0587 if j~=values
0588 zo = [zo zeros(j)];
0589 end
0590 end
0591
0592 setappdata(handles.main, 'zeros', zo);
0593 updateZerosList(handles);
0594
0595
0596 updateModelResp(handles);
0597
0598
0599
0600 function saveAObtn_Callback(hObject, eventdata, handles)
0601
0602
0603
0604
0605
0606 gain = str2double(get(handles.gainEdit, 'String'));
0607 poles = getappdata(handles.main, 'poles');
0608 zeros = getappdata(handles.main, 'zeros');
0609 pzm = pzmodel(gain, poles, zeros);
0610
0611 f1s = get(handles.f1Edit, 'String');
0612 f2s = get(handles.f2Edit, 'String');
0613 nfs = get(handles.nfEdit, 'String');
0614 fss = get(handles.fsEdit, 'String');
0615
0616 if isempty(f1s)
0617 f1 = getlowerFreq(pzm)/10;
0618 else
0619 f1 = str2double(f1s);
0620 end
0621 if isempty(f2s)
0622 f2 = getupperFreq(pzm)*10;
0623 else
0624 f2 = str2double(f2s);
0625 end
0626 if isempty(nfs)
0627 nf = 1000;
0628 else
0629 nf = str2double(nfs);
0630 end
0631
0632
0633 a = resp(pzm, f1, f2, nf);
0634
0635
0636 [filename, pathname] = uiputfile('*.xml', 'LTPDA XML file (*.xml)', 'Save as');
0637 if isequal(filename,0) || isequal(pathname,0)
0638 else
0639 fname = fullfile(pathname, filename);
0640 save(a, fname);
0641 end
0642