modulate delivers asynchronous result
1 view (last 30 days)
Show older comments
Hi,
I want to use modulate to get a pulse width modulated signal for a 200Hz signal at a sample rate of 8kHz.
this is my code
f_el=200;
fsw=8000;
stop_time=1/f_el;
step_fundamental=1/f_el/200;
t_fundamental=0:step_fundamental:stop_time;
sine=0.5*(sin(2*pi*f_el*t_fundamental)+1);
%% simple pwm modulation
y=modulate(sine,f_el,fsw,'pwm');
step_sw=stop_time/length(y);
t_sw=stop_time/length(y):step_sw:stop_time;
%
figure
hold on
plot(t_fundamental,sine,'linewidth',2)
plot(t_sw,y)
% plot(t_sw,1-y)
However, the result is an asynchronous pwm pattern with a larger area of "1" than "0".
Do you know why and can help me?
Thanks in advance
0 Comments
Accepted Answer
William Rose
on 4 Feb 2025
The result you observe is because the sampling rate (fsw in your code) is too low to support the extremes of modulation. Increwase fsw. When you do, you will see that the "blank" region near the peak of the sine wave becpmes narrower.
Another option is to adjust the modulating signal so that it does not get as close to unity and to zero. For example, keep it in the range 0.05 to 0.95.
4 Comments
William Rose
on 4 Feb 2025
The assymetry between ones and zeros is small when sine varies fomr 0.05 to 0.95. We add the lines
% nOne=sum(y==1); nZero=sum(y==0);
% fprintf('Length y=%d, number of ones=%d, number of zeros=%d.\n',...
% length(y),nOne,nZero)
and the result is
Length y=8040, number of ones=4118, number of zeros=3922.
Your code handles sampling rates in an unusual way, which may lead to misunderstanding. You have
% f_el=200;
% fsw=8000;
% stop_time=1/f_el;
% step_fundamental=1/f_el/200; % step_fundamental=1/(200^2)=1/(40000)
% t_fundamental=0:step_fundamental:stop_time;
% sine=0.5*(sin(2*pi*f_el*t_fundamental)+1);
Thefore the sinusoid is sampled at 40 kHz, and y, the pulse-width-modulated square wave, is sampled at 40 kHz*(8000/200)=1.6 MHz.
To improve the clarity of your code, you can define three frequencies: the frequency of the sinusoid, the sampling rate of the sinusoid, and the sampling rate of the square wave.
f0=200; % sinusoid frequency = modulation frequency
fc=40e3; % sampling frequency of sinusoid
fs=1600e3; % sampling frequncy of pulse-modulated square wave
tc=(0:fc/f0-1)/fc; % time base for sinusoid: one cycle
x=0.45*sin(2*pi*f0*tc)+0.5;
y=modulate(x,fc,fs,'pwm');
ts=(0:length(y)-1)/fs;
nOne=sum(y==1); nZero=sum(y==0);
fprintf('Length y=%d, number of ones=%d, number of zeros=%d.\n',...
length(y),nOne,nZero)
plot(tc,x,'-b',ts,y,'-r');
(The length of y is 40 points shorter in my version that in your version, because my "x" has 200 samples, and your "sine" has 201.)
William Rose
on 4 Feb 2025
Edited: William Rose
on 4 Feb 2025
[edit: fix spelling]
The asymmetry between ones and zeros in the pwm square wave is due to rounding and the finite sampling rate, fs. If you increase fs, the asymmetry decreases. Example: When fs=1600 kHz, the asymmetry is 4098 ones out of 8000, i.e. 51.23% ones. When fs=8000 kHz, the asymmetry is 20098 ones out of 40000, i.e. 50.24% ones.
nOne1=sum(y==1); nZero1=sum(y==0);
fprintf('Signal 1: %d points, %d ones (%.2f%%), %d zeros.\n',...
length(y),nOne1,nOne1*100/length(y),nZero1)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!