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