Generating a PWM Signal

			
				% generate a PWM signal

				Fs = 1.024e3; % sampling rate
				N = 1024; % no. of samples
				t = (0 : N-1)/Fs; % time vector

				Mc = 4; % sawtooth cycles
				Fc = (Fs/N)*Mc; % calc sawtooth freq

				c = 1.0 * sawtooth(2 * pi * Fc * t); % sawtooth signal

				Mm = 7; % sine cycles
				Fm = (Fs/N)*Mm; % calc sine freq

				m = 0.75 * sin(2 * pi * Fm * t); % sine modulator


				pwm = zeros(N); % init pwm signal
				for i = 1 : N
					if (m(i) >= c(i)) % if sine > sawtooth, generate edge
						pwm(i) = 0;
					else pwm(i) = 1;
					end
				end

				% plots
				subplot(3,1,1);
				plot(t, c);
				title('sawtooth');
				xlabel('time');
				ylabel('amplitude');

				subplot(3,1,2);
				plot(t, m);
				title('modulator');
				xlabel('time');
				ylabel('amplitude');

				subplot(3,1,3);
				plot(t, pwm);
				title('pwm signal');
				ylim([0 1.2]);
				xlabel('time');
				ylabel('amplitude');
			
		
© 2018 Copyright. For individual use only. No liability is accepted for any consequences of using information on Testips.com.