Square wave frequency modulation

Sonny120

Joined Nov 5, 2023
15
I am trying to modulate a square wave with a DC value. Simple stuff at least i thought. Basically as the DC value increases so should the frequency of the square wave and it should be represented on graphs. It's not working as intended as the DC value increases. It involves the use of Fourier. The code was done using matlab and follows:

Square wave frequency modulation
ThemeCopy
% Define parameters
t = linspace(0, 1, 1000); % Time vector
fc = 5; % Fundamental frequency (Hz)
kf = 0.1; % Frequency deviation constant
Vc = 1; % Amplitude of the carrier signal
Vdc_values = [0, 5, 10]; % Values of Vdc for the DC signals

% Generate carrier signal
vc = Vc/2;
for k = 1:2:10000
vc = vc + (2*Vc/(pi*k)) * cos(2*pi*k*fc*t - 90*pi/180);
end

% Plot carrier signal
figure;
plot(t, vc, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal for Frequency Modulation');
ylim([0, 1]); % Adjust ylim as needed
grid on;

% Frequency domain parameters
fs = 1000; % Sampling frequency
f = linspace(0, fs, 1000); % Frequency vector

% Generate and plot frequency-modulated signals separately for each Vdc
for i = 1:length(Vdc_values)
Vdc = Vdc_values(i);

% Generate frequency-modulated signal
v_fm = Vc/2;
for k = 1:2:1000
v_fm = v_fm + (2*Vc/(pi*k)) * cos(2*pi*2*k*fc*t - 90*pi/180 + 2*pi*kf*Vdc*t);
end

% Compute Fourier transform of frequency-modulated signal
V_fm = abs(fft(v_fm));

% Plot frequency-modulated signal in the time domain
figure;
subplot(2, 1, 1);
plot(t, v_fm, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title(['Frequency-Modulated Signal with Vdc = ', num2str(Vdc), ' (Time Domain)']);
ylim([0, 1]); % Adjust ylim as needed
grid on;

% Plot frequency-modulated signal in the frequency domain
subplot(2, 1, 2);
plot(f, V_fm, 'LineWidth', 2);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title(['Frequency-Modulated Signal with Vdc = ', num2str(Vdc), ' (Frequency Domain)']);
xlim([0, 200]); % Adjust xlim as needed
grid on;
end

As you can see as Vdc increases the signal breaks down.

Wendy

Joined Mar 24, 2008
23,457
Graphs without schematics are almost meaningless.

Sonny120

Joined Nov 5, 2023
15
Sorry If I was unclear at any point the above comes from the following math:

With Vc(t) being the square wave that is being modulated and v(t) the output of the modulation.
This math is being used as a concept before building the modulator.

Papabravo

Joined Feb 24, 2006
21,261
Graphs without schematics are almost meaningless.
There are no schematics in Matlab. It is all done in the abstract world of programming which is inaccessible to most people.

ApacheKid

Joined Jan 12, 2015
1,658
I am trying to modulate a square wave with a DC value. Simple stuff at least i thought. Basically as the DC value increases so should the frequency of the square wave and it should be represented on graphs. It's not working as intended as the DC value increases. It involves the use of Fourier. The code was done using matlab and follows:

Square wave frequency modulation
ThemeCopy
% Define parameters
t = linspace(0, 1, 1000); % Time vector
fc = 5; % Fundamental frequency (Hz)
kf = 0.1; % Frequency deviation constant
Vc = 1; % Amplitude of the carrier signal
Vdc_values = [0, 5, 10]; % Values of Vdc for the DC signals

% Generate carrier signal
vc = Vc/2;
for k = 1:2:10000
vc = vc + (2*Vc/(pi*k)) * cos(2*pi*k*fc*t - 90*pi/180);
end

% Plot carrier signal
figure;
plot(t, vc, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal for Frequency Modulation');
ylim([0, 1]); % Adjust ylim as needed
grid on;

% Frequency domain parameters
fs = 1000; % Sampling frequency
f = linspace(0, fs, 1000); % Frequency vector

% Generate and plot frequency-modulated signals separately for each Vdc
for i = 1:length(Vdc_values)
Vdc = Vdc_values(i);

% Generate frequency-modulated signal
v_fm = Vc/2;
for k = 1:2:1000
v_fm = v_fm + (2*Vc/(pi*k)) * cos(2*pi*2*k*fc*t - 90*pi/180 + 2*pi*kf*Vdc*t);
end

% Compute Fourier transform of frequency-modulated signal
V_fm = abs(fft(v_fm));

% Plot frequency-modulated signal in the time domain
figure;
subplot(2, 1, 1);
plot(t, v_fm, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title(['Frequency-Modulated Signal with Vdc = ', num2str(Vdc), ' (Time Domain)']);
ylim([0, 1]); % Adjust ylim as needed
grid on;

% Plot frequency-modulated signal in the frequency domain
subplot(2, 1, 2);
plot(f, V_fm, 'LineWidth', 2);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title(['Frequency-Modulated Signal with Vdc = ', num2str(Vdc), ' (Frequency Domain)']);
xlim([0, 200]); % Adjust xlim as needed
grid on;
end

As you can see as Vdc increases the signal breaks down.
Surely, the fact that a square wave is in fact the sum of multiple odd sinusoidal harmonics, means this is far from clear? You're not modulating a single frequency at all, you're attempting to modulate an infinite number of sinusoidal frequencies. The equations you shared pertain to a sinusoidal function, but a "square wave" is not a single sine frequency, but a sum of odd harmonics.

This sounds like a complicated way to solve a simple problem, why not frequency modulate a sinewave which is much easier?

DickCappels

Joined Aug 21, 2008
10,228
Buy yourself a VCO chip and be done with it. Lacking that, you can use the VCO portion of a PLL chip.

BobTPH

Joined Jun 5, 2013
9,141
Did not try to read the code.

But looking at your waveform, I think the modulation is way too fast with respect to the carrier. Try lowering the modulation frequency to more like 1% of the carrier frequency and you might see what you expect.

BobTPH

Joined Jun 5, 2013
9,141
Wait, are you trying to construct the square wave from its Fourier components? If so, I think you have the phase relationships off to get that waveform.

Hymie

Joined Mar 30, 2018
1,285
A simple frequency modulation circuit can be created based on an astable 555 timer circuit by varying the pin 5 (control voltage). Although increasing the voltage, lowers the frequency and the allowable (working) pin 5 voltage is from 0.45 to 0.9 Vsupply (rather than fixed at 0.66 Vsupply) - so the frequency modulation range is quite limited.

Papabravo

Joined Feb 24, 2006
21,261
A simple frequency modulation circuit can be created based on an astable 555 timer circuit by varying the pin 5 (control voltage). Although increasing the voltage, lowers the frequency and the allowable (working) pin 5 voltage is from 0.45 to 0.9 Vsupply (rather than fixed at 0.66 Vsupply) - so the frequency modulation range is quite limited.
The TS is not talking about building a circuit. He is trying to do it in MATLAB! For grins and giggles – I suppose.