How to use a triangle wave to modulate a square wave.
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
% Define parameters
t = linspace(0, 1, 1000); % Time vector
fc = 5; % Fundamental frequency (Hz)
kf = 0.1; % Frequency deviation constant
% Frequency domain parameters
fs = 1000; % Sampling frequency
f = linspace(0, fs, 1000); % Frequency vector
% Amplitude of triangle wave
amp = 10;
% Generate carrier signal (square wave)
vc_square = 0.5*square(2*pi*fc*t) + .5; % Square wave carrier
vc_triangle = amp*sawtooth(2*pi*fc*t, 0.5); % Triangle wave message
% Plot carrier signal
figure;
subplot(2, 1, 1);
plot(t, vc_square, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal (Square Wave)');
ylim([0, 1.01]); % Adjust ylim as needed
grid on;
% Plot message signal
subplot(2, 1, 2);
plot(t, vc_triangle, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal (Triangle Wave)');
grid on;
% Perform cumulative trapezoidal integration over the period
integral_values = 10*cumtrapz(t, vc_triangle);
% Generate frequency-modulated signal
fm_freq = fc + kf .* integral_values; % Modulated frequency
v_fm = square(2*pi*fm_freq.*t); % Frequency-modulated square wave
% 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']);
ylim([0, 1.01]); % 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']);
xlim([0, 200]); % Adjust xlim as needed
grid on;
The problem is this is not modulating correctly the frequency only linearly increases, instead of increasing and decreasing. There is something fundamenatally wrong with how I am modulating the signal.
0 comentarios
Respuestas (1)
Sameer
el 23 de Abr. de 2024
Hi Ashanni
From my understanding you wanted to modulate the frequency of a square wave using a triangle wave, intending for the square wave's frequency to increase and decrease in sync with the triangle wave's amplitude. However, your initial approach led to a linearly increasing frequency over time, rather than the intended modulation that mirrors the triangle wave's fluctuations.
The issue you're encountering stems from how the modulation is applied. In the above code, the modulation is applied through cumulative integration of the triangle wave, which inherently results in a signal that only increases linearly over time because the integral of a triangle wave over time increases monotonically within each cycle. This approach won't create a frequency modulation effect that follows the up-and-down pattern of the triangle wave directly.
For a more appropriate frequency modulation (FM) effect where the frequency of the square wave increases and decreases in accordance with the triangle wave, we can directly use the instantaneous amplitude of the triangle wave to modulate the frequency of the square wave, rather than using its integral. This way, the frequency of the square wave will increase when the triangle wave amplitude is positive and decrease when it's negative, creating the desired modulation effect.
Here's how you can adjust your code to achieve this:
% Define parameters
t = linspace(0, 1, 1000); % Time vector
fc = 5; % Carrier frequency (Hz)
kf = 100; % Frequency deviation constant (adjust as needed for visible effect)
fs = 1000; % Sampling frequency
f = linspace(0, fs, 1000); % Frequency vector
% Amplitude of triangle wave
amp = 1; % Adjust amplitude as needed
% Generate carrier signal (square wave)
vc_square = 0.5 * square(2 * pi * fc * t) + 0.5; % Square wave carrier
% Generate modulating signal (triangle wave)
vc_triangle = amp * sawtooth(2 * pi * fc * t, 0.5); % Triangle wave modulation
% Directly modulate the frequency of the square wave with the triangle wave
modulated_freq = fc + (kf * vc_triangle); % Instantaneous frequency
v_fm = 0.5 * square(2 * pi * cumtrapz(t, modulated_freq)) + 0.5; % FM square wave
% Plotting
figure;
subplot(3, 1, 1);
plot(t, vc_square, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal (Square Wave)');
ylim([0, 1.1]);
grid on;
subplot(3, 1, 2);
plot(t, vc_triangle, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Modulating Signal (Triangle Wave)');
grid on;
subplot(3, 1, 3);
plot(t, v_fm, 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Frequency-Modulated Square Wave');
ylim([0, 1.1]);
grid on;
In this revised approach, the “modulated_freq” variable directly represents the instantaneous frequency of the square wave as modulated by the triangle wave's amplitude. The “cumtrapz” function is used here to integrate this modulated frequency to get the phase of the square wave signal before applying the square function. The “kf” (frequency deviation constant) controls the extent of the frequency modulation, and you may need to adjust it to see a more pronounced effect.
I hope this helps!
Sameer
0 comentarios
Ver también
Categorías
Más información sobre Waveform Generation en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!