我正在尝试实现包络跟踪和脉宽调制。 在此示例中,我有一个 1 MHz 正弦波。我以 3MHz 采样该正弦波,每个采样将确定周期为 0.33us 的脉宽信号的占空比。
脉搏:
正弦波:
当我对脉冲信号进行 FFT 时,我期望看到 1MHz 频率。但看起来像这样
频率。领域:
我做错了什么?
这是我的代码:
pkg load signal
clear all;
close all;
% Parameters
f_sine = 1e6; % Frequency of the sine wave (1 MHz)
t_sine = 2e-6; % Time duration to plot (2 us)
f_sample = 3e6; % Sampling frequency (3 MHz)
t_sample = 0:1/f_sample:t_sine; % Time vector for sampled signal
t_cont = linspace(0, t_sine, 1000);
% Sine wave
sine_wave_cont = 1 + sin(2 * pi * f_sine * linspace(0, t_sine, 1000)); % Continuous sine wave from 0 to 2
sine_wave_sample = 1 + sin(2 * pi * f_sine * t_sample); % Sampled sine wave
% Compute percentage of sampled amplitude compared to max amplitude
max_amplitude = max(sine_wave_sample);
percentage_amplitude = (sine_wave_sample / max_amplitude) * 100;
% Square wave parameters
T_square = 0.33e-6; % Period of the square wave (0.33 us)
% Initialize the array for the square wave with varying duty cycles
square_wave_varying = [];
% Generate the square wave with varying duty cycle
for i = 1:length(percentage_amplitude)
duty_cycle = percentage_amplitude(i)*0.9;
t_current = linspace(0, T_square, 1000); % Time vector for the current period
if length(t_current) > 1
square_wave_current = 0.5 * (square(2 * pi * (1/T_square) * t_current, duty_cycle) + 1);
square_wave_varying = [square_wave_varying, square_wave_current(1:end-1)]; % Avoid overlapping periods
end
end
% Adjust time vector for the concatenated square wave
t_varying = linspace(0, length(square_wave_varying)/f_sample, length(square_wave_varying));
% Plot continuous sine wave
figure;
plot(linspace(0, t_sine, 1000) * 1e6, sine_wave_cont); % Time in us for x-axis
ylim([-0.5 2.5]);
xlabel('Time (us)');
ylabel('Amplitude');
title('Sine Wave at 1 MHz');
grid on; % Turn on grid
% Plot square wave with varying duty cycle
figure;
plot(t_varying * 1e3, square_wave_varying); % Time in us for x-axis
ylim([-0.2 1.4]);
xlabel('Time (us)');
ylabel('Amplitude');
title('Square Wave with Varying Duty Cycle');
grid on; % Turn on grid
% Plot percentage amplitude
figure;
stem(t_sample * 1e6, percentage_amplitude, 'filled'); % Time in us for x-axis
ylim([0 110]);
xlabel('Time (us)');
ylabel('Percentage Amplitude');
title('Percentage Amplitude of Sampled Sine Wave at 3 MHz');
grid on; % Turn on grid
% Frequency domain analysis
N = length(square_wave_varying); % Number of points
f = (0:N-1)*(f_sample/N); % Frequency range
Y = fft(square_wave_varying); % Compute FFT
% Plot frequency spectrum
figure;
plot(f(1:N/2)/1e6, abs(Y(1:N/2))); % Plot single-sided amplitude spectrum
xlabel('Frequency (MHz)');
ylabel('Magnitude');
title('Frequency Spectrum of Square Wave with Varying Duty Cycle');
grid on; % Turn on grid
我相信您混淆了正弦波的频率和信号的采样频率。
正弦波的频率是其周期的倒数。它直接表示信号每秒有多少个周期。例如,如果您在 1 秒内绘制 2 赫兹正弦波,您将看到两个周期:
另一方面,采样频率表示每秒将采集给定信号的次数(如在现实生活中的情况下测量的那样)。以前一个示例为例,下图表示以 8Hz、32Hz 和 256Hz 采样的相同正弦波。请注意,随着采样频率相对于正弦波频率变大,信号的“平滑度”会变得更好:
现在事情变得更加清晰了,让我们浏览一下您的代码,并进行一些更改:
% Parameters
f_sine = 1e6; % Frequency of the sine wave (1 MHz)
t_sine = 2e-6; % Time duration to plot (2 us)
f_sample = 64e6; % Sampling frequency (64 MHz <-- that means 64 points per period, it's a minimum)
t_sample = 0:1/f_sample:t_sine; % Time vector for sampled signal
% Sine wave
sine_wave_sample = 1 + sin(2 * pi * f_sine * t_sample); % Sampled sine wave
您应该只保留
sine_wave_sample
变量。它是连续正弦波的离散表示。
剧情如下:
% Plot sampled sine wave
figure;
plot(t_sample*1e6, sine_wave_sample); % Time in us for x-axis
ylim([-0.5 2.5]);
xlabel('Time (us)');
ylabel('Amplitude');
title('Sine Wave of frequency 1 MHz, sampled at 64 MHz');
grid on; % Turn on grid
set(gcf,'color','w')
set(gca,'fontsize',10)
现在您可以继续构建方波集合:
% Compute percentage of sampled amplitude compared to max amplitude
max_amplitude = max(sine_wave_sample);
percentage_amplitude = (sine_wave_sample / max_amplitude) * 100;
% Square wave parameters
T_square = 0.33e-6; % Period of the square wave (0.33 us)
% Initialize the array for the square wave with varying duty cycles
square_wave_varying = [];
% Generate the square wave with varying duty cycle
for i = 1:length(percentage_amplitude)
duty_cycle = percentage_amplitude(i)*0.9;
square_wave_current = 0.5 * (square(2 * pi * (1/T_square) * t_sample(2:end), duty_cycle) + 1);
square_wave_varying = [square_wave_varying, square_wave_current];
end
您可以绘制百分比幅度(请注意,使用 64 MHz 采样频率,它看起来更好):
figure;
stem(t_sample * 1e6, percentage_amplitude, 'filled'); % Time in us for x-axis
ylim([0 110]);
xlabel('Time (us)');
ylabel('Percentage Amplitude');
title('Percentage Amplitude of Sine Wave of frequency 1 MHz, sampled at 64 MHz');
grid on; % Turn on grid
set(gcf,'color','w')
set(gca,'fontsize',10)
与您的方块相对应的时间信号是使用
time_sample
向量计算的:
% Time vector for the concatenated square wave
t_varying_stacked = (0:(length(percentage_amplitude)-1))*max(t_sample)+t_sample(2:end).';
t_varying = t_varying_stacked(:);
它有很多点,但放大图你可以清楚地看到方波持续时间的增加/减少:
% Plot square wave with varying duty cycle
figure;
plot(t_varying * 1e6, square_wave_varying.'); % Time in us for x-axis
ylim([-0.2 1.4]);
xlabel('Time (us)');
ylabel('Amplitude');
title('Square Wave with Varying Duty Cycle');
grid on; % Turn on grid
set(gcf,'color','w')
set(gca,'fontsize',10)
最后一部分是FFT,您实际上已经正确计算了:
% Frequency domain analysis
N = length(square_wave_varying); % Number of points
f = (0:N-1)*(f_sample/N); % Frequency range
Y = fft(square_wave_varying); % Compute FFT
% Plot frequency spectrum
figure;
plot(f(1:N/2)/1e6, abs(Y(1:N/2))); % Plot single-sided amplitude spectrum
xlabel('Frequency (MHz)');
ylabel('Magnitude');
title('Frequency Spectrum of Square Wave with Varying Duty Cycle');
grid on; % Turn on grid
set(gcf,'color','w')
set(gca,'fontsize',10)
如果放大频谱图,您可以看到以下频率:
对应于信号平均值的零频率
对应于 0.33us 周期方波频率的 3 MHz 频率(及其倍数)
对应于占空比正弦波调制的 1 MHz 频率(和倍数)
与正弦波给出的平均调制相对应的 0.5 MHz 频率(和倍数)