% Dionysios Aliprantis
% Iowa State University
% E E 452. Electric Machines and Power Electronic Drives
% Revision: 1/30/2012

% Script for analysis of dc chopper waveform

clear all, clc

D = 0.5;
fs = 5000;    % switching frequency (Hz)
L = 0.08;     % H
R = 300;      % ohm
C = 1e-6;     % F
tstop = 0.25;

% sim('Buck_without_C')
sim('Buck_with_C')
t = vout(:,1);
vo = vout(:,2);


Nperiods = 5; % number of periods to use for calculations 
ind = find(t >= t(end)-Nperiods/fs);
dt = t(2)-t(1);
T = t(ind(end))-t(ind(1));  % this is NOT one period, but Nperiods

% interpolate data for better accuracy
tint = linspace(t(ind(1)),t(ind(1))+T,Nperiods*1000);
voint = interp1(t(ind),vo(ind),tint,'linear');
dtint = tint(2)-tint(1);

figure(1), clf
plot(t(ind),vo(ind),'.')
hold on
plot(tint,voint)
xlabel('time (s)')
ylabel('v_o (V)')

% calculate figures of merit
Voavg = mean(voint)
Vorms = sqrt( sum(voint.^2)*dtint/T )
RFvo = sqrt(Vorms^2 - Voavg^2)/Voavg



