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