You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
clear; clc; close all;
|
|
|
|
% 1. 参数
|
|
fs = 10000; % 采样频率 10 kHz
|
|
t_total = 1; % 时间长度 1 s
|
|
f0 = 1000; % 基频 1 kHz
|
|
t = 0:1/fs:t_total-1/fs; % 时间轴
|
|
N = length(t)
|
|
|
|
% 2. 基频信号
|
|
x = sin(2*pi*f0*t);
|
|
|
|
% 3. 线性系统、非线性系统
|
|
y1 = 2 * x; % 线性
|
|
y2 = x + 0.5*x.^2 + 0.1*x.^3; % 非线性
|
|
|
|
% 4. 时域图
|
|
figure('Color','w');
|
|
idx = t <= 0.01; % 按要求取前10ms
|
|
subplot(2,1,1);
|
|
plot(t(idx)*1000, x(idx), 'k'); hold on;
|
|
plot(t(idx)*1000, y1(idx), 'b');
|
|
plot(t(idx)*1000, y2(idx), 'r');
|
|
xlabel('时间/ms');
|
|
ylabel('幅度');
|
|
title('时域');
|
|
legend('x(t)','y1(t)','y2(t)');
|
|
grid on;
|
|
|
|
% 5. FFT
|
|
f = (0:N-1)*fs/N;
|
|
|
|
X = abs(fft(x, N)) / length(x);
|
|
Y1 = abs(fft(y1, N)) / length(y1);
|
|
Y2 = abs(fft(y2, N)) / length(y2);
|
|
|
|
% 只画正频率
|
|
X = X(1:N/2+1);
|
|
Y1 = Y1(1:N/2+1);
|
|
Y2 = Y2(1:N/2+1);
|
|
f = f(1:N/2+1);
|
|
|
|
subplot(2,1,2);
|
|
plot(f, Y1, 'b', 'LineWidth',1.5); hold on;
|
|
plot(f, Y2, 'r', 'LineWidth',1.5);
|
|
xlim([0, 4500]);
|
|
xlabel('频率/Hz');
|
|
ylabel('幅度');
|
|
title('频域单边幅度谱');
|
|
legend('线性','非线性');
|
|
|
|
% 6. 谐波幅值输出
|
|
f1_idx = find(f>=1000,1);
|
|
f2_idx = find(f>=2000,1);
|
|
f3_idx = find(f>=3000,1);
|
|
|
|
fprintf('基频 1kHz 幅值:%.4f\n', Y2(f1_idx));
|
|
fprintf('二次谐波 2kHz 幅值:%.4f\n', Y2(f2_idx));
|
|
fprintf('三次谐波 3kHz 幅值:%.4f\n', Y2(f3_idx)); |