File:Pwm.gif
Summary
| Description |
Deutsch: Pulsweiten-Modulation |
| Date | |
| Source | Own work |
| Author | Mik81 |
| GIF development | Category:PNG created with Octave#Pwm.gif File:.svg and with ImageMagick. Category:Created also with ImageMagick |
en:GNU Octave
% GNU Octave / MATLAB
% Benutzer:mik81 @ de.wikipedia.org
%
%
% Große Frage:
% Welche "amplitude" haben die Spektren?
%
%
printf("mik81 @ de.wikipedia.org\n\n");
printf("Spectrum of rectengular waveform\n\n");
stepsPerPeriod = 128; % 2^8
periods = 16; % 2^4
steps = periods*stepsPerPeriod;
timebase = 0:1:steps-1;
size(timebase)
% dutycycle = 0.05;
mkdir("./data");
axis("labelxy", "ticxy", "on");
for count=1:1:40
countString = sprintf("%03i", count);
dutycycle = count/80;
dutycycleString = sprintf("%0.3f", dutycycle);
realDutycycle = 0;
%
% set up waveform
%
printf("Calculating waveform: %i\n", count);
for period=0:1:periods-1
for step=0:1:stepsPerPeriod-1
if ( step >= dutycycle *stepsPerPeriod)
square = [square, 0.];
if ( realDutycycle == 0)
realDutycycle = step/stepsPerPeriod
dutycycle
endif
else
if (period == 0 && step == 0)
square = [+1.];
else
square = [square, +1.];
endif
endif
endfor
endfor
for step = 0:1:steps-1
if ( step == 0)
sinus = [sin(2*pi*0)];
else
sinus = [sinus, sin(2*pi*step/stepsPerPeriod)];
endif
endfor
sinus = 0:1:steps-1;
sinus = sin(2*pi*sinus/stepsPerPeriod);
% size(sinus)
% size(square)
plot((0:1:steps/(0.25*periods)-1)/stepsPerPeriod
, square(1,1:steps/(0.25*periods)), [";Tastgrad: " , dutycycleString, ";"]);
axis ([0, 0.25*periods, -0.2, +1.2]);
axis ("nolabel");
axis("labely", "ticxy");
xlabel ("");
__gnuplot_set__ size 0.42,0.42;
print(["./data/waveform", countString, ".png"], "-dpng");
__gnuplot_set__ size 1.,1.;
%
% DFT
%
printf("Calculating DFT: %i\n", count);
freqSteps = steps/2;
%
% Frequenz = freq*Samplerate*steps
% n * Fenster
% remove DC-part
%square = square .- sum(square(1,:));
for freq=0:1:freqSteps
pad = square .* sin(freq*2*pi*timebase/length(timebase));
dft(1, freq+1) = sum( pad(1,:) );
pad = square .* cos(freq*2*pi*timebase/length(timebase));
% cos^2 + sin^2
dft( 1, freq+1) = dft(1, freq+1)^2 + sum( pad(1,:) )^2;
% normalize = max(dft(1, freq+1), normalize);
pad = sinus .* sin(freq*2*pi*timebase/length(timebase));
dftSignal(1, freq+1) = sum( pad(1,:) );
pad = sinus .* cos(freq*2*pi*timebase/length(timebase));
% cos^2 + sin^2
dftSignal(1, freq+1) = dftSignal( 1, freq+1)^2 + sum( pad(1,:) )^2;
endfor
dft = sqrt(dft);
dft = dft/(periods*stepsPerPeriod);
dftSignal = dftSignal/(periods*stepsPerPeriod);
%envelop = dft(1,1) * abs(sinc((0:1/10:freqSteps)*dutycycle));
envelop = dft(1,1) * abs(sinc((0:1/10:freqSteps)*realDutycycle));
%size(dft)
%semilogx((0:1:steps-1)/stepsPerPeriod, square);
%semilogx((1:1:freqSteps-1)/periods, dft(2:freqSteps,1));
% plot
plot((0:1:freqSteps)/periods, dft(1, 1:freqSteps+1), ["+1;Spektallinien;"]
, (0:1:freqSteps)/periods, dft(1, 1:freqSteps+1), ["^1;;"]
, (0:1/10:freqSteps), envelop, ";Einhuellende;");
dftSelected = [dft(1,1)];
for selected=periods+1:periods:freqSteps+1
dftSelected = [dftSelected, dft(1,selected)];
endfor
%size(dftSelected)
%size([0, (periods+1:periods:freqSteps+1)/periods])
plot([0, (periods+1:periods:freqSteps+1)/periods-1/periods], dftSelected(1, :), ["+1;Spektallinien;"]
,[0, (periods+1:periods:freqSteps+1)/periods-1/periods], dftSelected(1, :), ["^1;;"]
, (0:1/10:freqSteps), envelop, ";Einhuellende;");
%semilogx((1:1:freqSteps-1)/periods, dftSignal(2:freqSteps,1));
%plot((1:1:freqSteps/8-1)/periods, dftSignal(2:freqSteps/8,1));
axis ([0, 20, 0, 0.6]);
axis("labelxy", "ticxy", "on");
xlabel ("Vielfache der Grundfrequenz");
%axis ("ticx");
print(["./data/spectrum", countString, ".png"], "-dpng");
%if ( freq == 8)
% fetch = pad;
% sum(pad(1,:))
% dft(1, freq+1)
% endif
%plot(0:1:length(fetch)-1, fetch, [";fetch", dutycycleString ,";"] );
%print(["./data/fetch", countString, ".png"], "-dpng");
endfor
Bash/ImageMagick
#! /bin/bash
echo "Script of mik81 @ de.wikipedia.org"
#vor00() {
# let "${{#1}==2" && $1=0${{1}
# let "${{#1}==1" && $1=00${{1}
# let "${{#1}==0" && $1=000
# echo $1
#}
#echo (vor00 5))
((i=1))
((m=40))
rm ./data/*
octave ./pulsweite.m
while ((i<=m)); do
case $i in
??) composite -gravity East ./data/waveform0$i.png ./data/spectrum0$i.png ./data/composite0$i.png;;
?) composite -gravity East ./data/waveform00$i.png ./data/spectrum00$i.png ./data/composite00$i.png;;
*) break;;
esac
((i=$i+1))
done
((i=41))
((m=45))
while ((i<m)); do
#octave ./pulsweite.m
cp ./data/composite040.png ./data/composite0$i.png
((i=$i+1))
done
convert -delay 25 ./data/composite*.png ./pwm.gif
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
| This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication. | |
| The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
|