?? pwm_adc.m
字號:
% PWM modulation simulation
% written by
% clear all;
close all;
% parameters
T = 0.001; %modulation period, unit: second
Resolution = 8; %the bit width used to control the duty cycle, unit: bit
Grid_num = 2^Resolution-1;
Tpwm = T/(2^Resolution-1); %PWM period, unit: second
Fpwm = 1/Tpwm;
Len = 100; %the lenght of the test signal, unit: modualtion period
% modulated signals
% sinusoid
Ft = 100; %frequency of the sinusoid
t = 0:T:T*(Len-1);
Wt = round(Grid_num*(sin(2*pi*Ft*t)+1)/2);
% constant DC
% Ratio = 0.7; % ratio of the constant DC to the full range
% t = 0:T:T*(Len-1);
% Wt(1:Len) = round(Ratio*Grid_num);
% generate PWM signal
tpwm = 0:Tpwm:(Grid_num*Len-1)*Tpwm;
Wpwm = zeros(1, Len*Grid_num);
for cycle=1:Len
for i=1:Grid_num
if(i<=Wt(cycle))
Wpwm((cycle-1)*Grid_num+i) = 1;
else
Wpwm((cycle-1)*Grid_num+i) = 0;
end
end
end
plot(tpwm, Wpwm);grid on;
axis([0 (Grid_num*Len-1)*Tpwm -0.5 2]);
% frequency domain analyse
NFFT = 2^nextpow2(Len*Grid_num);
DFT = fft(Wpwm, NFFT)/NFFT;
f = Fpwm/2*linspace(0,1,NFFT/2+1);
figure;
plot(f,2*abs(DFT(1:NFFT/2+1)));grid on;
%% filtering
figure;
%FIR
% Wn = 1/(Fpwm/2)*500; %calculate the coefficient of the FIR low pass filter
% b = fir1(Grid_num*5, Wn); %build a FIR low pass filter with cutoff frequency
% L_PWM = filter(b,1,Wpwm);
% plot(tpwm, L_PWM);grid on;hold on;
% plot(t, Wt/Grid_num, 'r'); %original signal
%butterworth IIR filter
load('filter_iir_17.mat'); %lodad the coefficients calculated from FDATOOL
L_PWM = filter(Hd,Wpwm);
plot(tpwm, L_PWM);grid on;hold on;
plot(t, Wt/Grid_num, 'r'); %original signal
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -