%% mcb_pmsm_freq_est_plot

% Model         :   Online Frequency estimation for PMSM motor
% Description   :   Plot the Bode plot for online frequency estimation
%               :   mcb_pmsm_freq_est_f28379d simulation outputs raw
%               :   frequency data to workspace.
%               :   This scripts reads the raw frequency data and frequency
%               :   array from mcb_pmsm_fre_est_f28379d_data and plot the
%               :   frequency spectrum of the plant.
%               :   Bode plot for out.Spdfreqdata, fre.Spd_freq
%               :   Bode plot for out.Idfreqdata, fre.I_freq
%               :   Bode plot for out.Iqfreqdata, fre.I_freq

% Copyright 2020 The MathWorks, Inc.


if exist('out','var')
    % verify simulation results are available in workspace
    
    opts=bodeoptions;
    opts.FreqUnits='Hz';
    opts.Grid='On';
    
    %% plot magnitude for frequency data
    subplot(3,2,1);
    opts.MagVisible ='On';
    opts.PhaseVisible ='Off';
    sys_sim_spd = frd(out.Spdfreqdata,fre.spd_freq*2*pi);
    bodeplot(sys_sim_spd,'b.-',opts);
    title('Speed loop (mag) - Iq ref / Speed meas');
    
    %% plot phase for frequency data
    subplot(3,2,2);
    opts.MagVisible ='Off';
    opts.PhaseVisible ='On';
    sys_sim_spd = frd(out.Spdfreqdata,fre.spd_freq*2*pi);
    bodeplot(sys_sim_spd,'b.-',opts);
    title('Speed loop (phase) - Iq ref / Speed meas');
    
    %% plot magnitude for Id Current loop
    subplot(3,2,3);
    opts.MagVisible ='On';
    opts.PhaseVisible ='Off';
    sys_sim_id = frd(out.Idfreqdata,fre.i_freq*2*pi);
    bode(sys_sim_id,'b.-',opts);
    title('Id Current loop (mag) - Vd out / Id meas');
    
    %% plot phase for Id Current loop
    subplot(3,2,4);
    opts.MagVisible ='Off';
    opts.PhaseVisible ='On';
    sys_sim_id = frd(out.Idfreqdata,fre.i_freq*2*pi);
    bode(sys_sim_id,'b.-',opts);
    title('Id Current loop (phase) - Vd out / Id meas');
    
    %% plot magnitude for Iq Current loop
    subplot(3,2,5);
    opts.MagVisible ='On';
    opts.PhaseVisible ='Off';
    sys_sim_iq = frd(out.Iqfreqdata,fre.i_freq*2*pi);
    bode(sys_sim_iq,'b.-',opts);
    title('Iq Current loop (mag) - Vq out / Iq meas');
    
    %% plot Phase for Iq Current loop
    subplot(3,2,6);
    opts.MagVisible ='Off';
    opts.PhaseVisible ='On';
    sys_sim_iq = frd(out.Iqfreqdata,fre.i_freq*2*pi);
    bode(sys_sim_iq,'b.-',opts);
    title('Iq Current loop (Phase) - Vq out / Iq meas');
    
else
    % error simulation results are not available in workspace
    error(message('mcb:blocks:NoSimData'));
end
