----------------------------------------------------------------------------------------------------------------------
interp1
示例代码
% 创建示例数据(非均匀采样)
f = [10, 20, 50, 100, 200, 500, 1000]; % 非均匀频率点
H = [0.9+0.1i, 0.8+0.2i, 0.7+0.3i, 0.5+0.4i, 0.3+0.2i, 0.1+0.1i, 0.05+0.05i];
% 创建均匀频率向量
f_uniform = linspace(10, 1000, 100); % 100个均匀分布的点
% 分别对实部和虚部进行插值
H_real_interp = interp1(f, real(H), f_uniform, 'pchip', 'extrap');
H_imag_interp = interp1(f, imag(H), f_uniform, 'pchip', 'extrap');
% 组合成复数频率响应
H_uniform = H_real_interp + 1i*H_imag_interp;
% 绘制原始数据和插值结果
figure;
subplot(2,1,1);
plot(f, real(H), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 8);
hold on;
plot(f_uniform, H_real_interp, 'b-');
xlabel('频率 (Hz)');
ylabel('实部');
title('频率响应实部插值');
legend('原始数据', '插值结果');
grid on;
subplot(2,1,2);
plot(f, imag(H), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 8);
hold on;
plot(f_uniform, H_imag_interp, 'b-');
xlabel('频率 (Hz)');
ylabel('虚部');
title('频率响应虚部插值');
legend('原始数据', '插值结果');
grid on;
----------------------------------------------------------------------------------------------------------------------
s_t_full = cumsum(h_t) * dt; % 使用累积和近似积分
----------------------------------------------------------------------------------------------------------------------
z = -1 + 1i; % 创建一个复数,位于第二象限
theta = angle(z); % 计算相位角
disp(theta); % 输出结果为 2.3562 弧度,即 3*pi/4 (135度)
% 验证
disp(atan2(imag(z), real(z))); % 输出同样为 2.3562
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
% 创建一个连续增长的相位(例如,从 0 到 5π)
true_phase = linspace(0, 5*pi, 1000);
% 用 angle 函数模拟测量到的相位(包裹在 -pi 到 pi 之间)
wrapped_phase = mod(true_phase + pi, 2*pi) - pi;
% 使用 unwrap 进行校正
unwrapped_phase = unwrap(wrapped_phase);
% 绘图比较
figure;
plot(true_phase, 'b-', 'LineWidth', 2, 'DisplayName', 'True Phase');
hold on;
plot(wrapped_phase, 'r--', 'DisplayName', 'Wrapped Phase (angle)');
plot(unwrapped_phase, 'g-.', 'LineWidth', 1.5, 'DisplayName', 'Unwrapped Phase');
hold off;
legend('show');
xlabel('Sample');
ylabel('Phase (radians)');
title('Phase Wrapping and Unwrapping');