Skip to content

Commit 56de88e

Browse files
committed
[PitchGain.m] Refactored the frequency response calculations
1 parent 413d632 commit 56de88e

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

subfunctions/PitchGain.m

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -534,21 +534,20 @@ function BodePlot(handles, undock, exportData)
534534
LoopGain(1,i) = series(ControllerLG(:,i), Plant(:,i));
535535
end
536536

537-
w_limit = 2; % change this limit if one of the margins can't be found
538-
w = logspace(-w_limit, w_limit,1000);
539-
Controller_FRF = freqresp(Controller, w);
540-
ControllerLG_FRF = freqresp(ControllerLG, w);
541-
Controller_MagResponse = mag2db(squeeze(abs(Controller_FRF)))';
542-
Controller_PhaseResponse = angle(squeeze(Controller_FRF))'*180/pi;
543-
544-
LoopGainFRF = freqresp(LoopGain, w);
545-
LoopGainMagResponseAbs = squeeze(abs(LoopGainFRF))';
546-
LoopGainMagResponse = mag2db(LoopGainMagResponseAbs);
547-
LoopGainPhaseResponse = angle(squeeze(LoopGainFRF))'*180/pi;
537+
w_llimit = -2; % change this limit if one of the margins can't be found
538+
w_ulimit = 2;
539+
w = logspace(w_llimit, w_ulimit, 1000);
548540

549-
PlantFRF = freqresp(Plant, w);
550-
PlantMagResponse = mag2db(squeeze(abs(PlantFRF)))';
551-
PlantPhaseResponse = angle(squeeze(PlantFRF))'*180/pi;
541+
[ControllerLG_FRF, Controller_MagResponse, Controller_PhaseResponse] = ...
542+
calculateFreqResp(Controller, w);
543+
Controller_FRF = freqresp(Controller, w);
544+
545+
[LoopGainFRF, LoopGainMagResponse, LoopGainPhaseResponse] = ...
546+
calculateFreqResp(LoopGain, w);
547+
LoopGainMagResponseAbs = db2mag(LoopGainMagResponse); % for margins calculation
548+
549+
[PlantFRF, PlantMagResponse, PlantPhaseResponse] = ...
550+
calculateFreqResp(Plant, w);
552551

553552
% Ensure the data is always saved column-wise for single line plots
554553
if size(Controller_MagResponse, 1) == 1
@@ -590,8 +589,8 @@ function BodePlot(handles, undock, exportData)
590589
legendMargins(:,6) = {' [deg] '};
591590

592591
if length(handles.SelectedListboxContents) == 1
593-
legendMargins{2} = num2str(GM);
594-
legendMargins{5} = num2str(PM);
592+
legendMargins{2} = num2str(GM,'%.2f');
593+
legendMargins{5} = num2str(PM,'%.2f');
595594
legendMargins = strcat(legendMargins{1}, legendMargins{2}, ...
596595
legendMargins{3}, legendMargins{4}, legendMargins{5}, ...
597596
legendMargins{6});
@@ -634,7 +633,7 @@ function BodePlot(handles, undock, exportData)
634633
hold on
635634
end
636635
if get(handles.PlotLoopGain_checkbox, 'Value')
637-
if get(handles.PlotGMPM_checkbox,'value')
636+
if get(handles.PlotGMPM_checkbox,'value') && (log10(GMFreq(i)) <= w_ulimit) && (log10(GMFreq(i)) >= w_llimit)
638637
h(i) = semilogx([GMFreq(i) GMFreq(i)], [0 -GM(i)], 'Color', [0 0.8 0], 'LineStyle', plotLineStyle{2}, 'LineWidth', plotLineWidth(1));
639638
h(i) = semilogx(GMFreq(i), -GM(i), 'o', 'Color', [0 0.8 0], 'LineStyle', plotLineStyle{1}, 'LineWidth', plotLineWidth(1));
640639

@@ -674,7 +673,7 @@ function BodePlot(handles, undock, exportData)
674673
if get(handles.PlotLoopGain_checkbox, 'Value')
675674
semilogx(w, LoopGainPhaseResponse(:,i), 'Color', ones(1,3)*plotCol(i), 'LineStyle', plotLineStyle{3}, 'LineWidth', plotLineWidth(3));
676675
hold on
677-
if get(handles.PlotGMPM_checkbox,'value')
676+
if get(handles.PlotGMPM_checkbox,'value') && (log10(PMFreq(i)) <= w_ulimit) && (log10(PMFreq(i)) >= w_llimit)
678677
if PM(i) >= 0
679678
dotPM = -180 + PM(i);
680679
else
@@ -701,6 +700,11 @@ function BodePlot(handles, undock, exportData)
701700
end
702701
end
703702

703+
function [frf, MagResponse, PhaseResponse] = calculateFreqResp(transfer_function, w)
704+
frf = freqresp(transfer_function, w);
705+
MagResponse = mag2db(squeeze(abs(frf)))';
706+
PhaseResponse = angle(squeeze(frf))'*180/pi;
707+
704708
% --- Executes on button press in LoadLinMat_pushbutton.
705709
function LoadLinMat_pushbutton_Callback(hObject, eventdata, handles)
706710
% Get file name

0 commit comments

Comments
 (0)