写出这些实验的MATLAB代码
时间: 2024-11-23 15:15:28 浏览: 8
以下是针对《信号与系统实验2024-刘晓莉.pdf》中的各个实验题目的MATLAB代码示例:
### 实验一 连续时间信号与系统分析
#### 实验题目:信号的频移和滤波分析
```matlab
% 定义参数
sigma = 1;
w0 = 2000 * pi;
% 绘制系统幅频特性曲线
w = linspace(-10*w0, 10*w0, 1000);
H = (sigma^2 + w.^2) ./ (w0^2 + w.^2);
figure;
plot(w / (2*pi), abs(H));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('System Frequency Response');
% 激励信号 1: x1(t) = cos(w0*t)
t = linspace(0, 0.01, 1000);
x1 = cos(w0 * t);
% 计算系统输出稳态响应
y1 = conv(x1, real(ifft(H)), 'same');
figure;
subplot(2, 1, 1);
plot(t, x1);
xlabel('Time (s)');
ylabel('Amplitude');
title('Input Signal x1(t)');
subplot(2, 1, 2);
plot(t, y1);
xlabel('Time (s)');
ylabel('Amplitude');
title('Output Signal y1(t)');
% 激励信号 2: x2(t) = cos(w0*t) * cos(c*t)
c = 20000;
x2 = cos(w0 * t) .* cos(c * t);
% 计算系统输出稳态响应
y2 = conv(x2, real(ifft(H)), 'same');
figure;
subplot(2, 1, 1);
plot(t, x2);
xlabel('Time (s)');
ylabel('Amplitude');
title('Input Signal x2(t)');
subplot(2, 1, 2);
plot(t, y2);
xlabel('Time (s)');
ylabel('Amplitude');
title('Output Signal y2(t)');
```
#### 实验题目:RC电路的频域分析
```matlab
% 定义输入信号
t = linspace(0, 0.02, 1000);
x = 10 * sin(100 * pi * t);
% 绘制时域波形
figure;
plot(t, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Input Signal x(t) in Time Domain');
% 计算频域波形
X = fft(x);
f = linspace(0, 50, length(X));
figure;
plot(f, abs(X));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Input Signal x(t) in Frequency Domain');
% 不同RC值下的频响特性
RC_values = [1, 0.1, 0.01];
for i = 1:length(RC_values)
RC = RC_values(i);
w = linspace(0, 1000, 1000);
H = 1 ./ (1 + 1i * w * RC);
figure;
plot(w, abs(H));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title(['Frequency Response for RC = ', num2str(RC)]);
end
% 计算不同RC值下的输出响应
for i = 1:length(RC_values)
RC = RC_values(i);
Y = X .* ifftshift(1 ./ (1 + 1i * f * RC));
y = real(ifft(Y));
figure;
subplot(2, 1, 1);
plot(t, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Input Signal x(t)');
subplot(2, 1, 2);
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title(['Output Signal y(t) for RC = ', num2str(RC)]);
end
% 计算直流分量
dc_x = mean(x);
dc_y = zeros(length(RC_values), 1);
for i = 1:length(RC_values)
RC = RC_values(i);
Y = X .* ifftshift(1 ./ (1 + 1i * f * RC));
y = real(ifft(Y));
dc_y(i) = mean(y);
end
disp(['DC component of x(t): ', num2str(dc_x)]);
disp('DC components of y(t) for different RC values:');
disp(dc_y);
```
### 实验二 离散时间信号与系统分析
#### 实验题目:DFT时移特性的研究
```matlab
% 定义参数
N_values = [32, 64, 80];
% 计算DFT
for N = N_values
n = 0:N-1;
x = sin(pi/3 * n) .* (heaviside(n) - heaviside(n - N));
% 绘制时域波形和频谱
figure;
subplot(2, 1, 1);
stem(n, x);
xlabel('n');
ylabel('Amplitude');
title(['Input Signal x(n) for N = ', num2str(N)]);
X = fft(x);
k = 0:N-1;
subplot(2, 1, 2);
stem(k, abs(X));
xlabel('k');
ylabel('Magnitude');
title(['DFT Magnitude Spectrum for N = ', num2str(N)]);
end
% 非因果信号
for N = N_values
n = 0:N-1;
x = sin(pi/3 * n) .* (heaviside(n) - heaviside(n - N));
x1 = circshift(x, N/4);
% 绘制时域波形和频谱
figure;
subplot(2, 1, 1);
stem(n, x1);
xlabel('n');
ylabel('Amplitude');
title(['Shifted Signal x1(n) for N = ', num2str(N)]);
X1 = fft(x1);
k = 0:N-1;
subplot(2, 1, 2);
stem(k, abs(X1));
xlabel('k');
ylabel('Magnitude');
title(['DFT Magnitude Spectrum for N = ', num2str(N)]);
end
% 改变时间右移点数
N = 64; % 以N=64为例
time_shifts = [N/4, 2*N/4, 3*N/4, -N/4, -2*N/4, -3*N/4];
for shift = time_shifts
n = 0:N-1;
x = sin(pi/3 * n) .* (heaviside(n) - heaviside(n - N));
x1 = circshift(x, shift);
% 绘制时域波形和频谱
figure;
subplot(2, 1, 1);
stem(n, x1);
xlabel('n');
ylabel('Amplitude');
title(['Shifted Signal x1(n) with Shift = ', num2str(shift)]);
X1 = fft(x1);
k = 0:N-1;
subplot(2, 1, 2);
stem(k, abs(X1));
xlabel('k');
ylabel('Magnitude');
title(['DFT Magnitude Spectrum with Shift = ', num2str(shift)]);
end
```
#### 实验题目:用DFT分析连续时间信号的频谱
```matlab
% 定义模拟信号
fs = 1000; % 采样频率
T = 1/fs; % 采样周期
t = 0:T:1-T; % 时间向量
x = 10 * cos(2 * pi * 49 * t) + 15 * cos(2 * pi * 51 * t);
% 绘制时域波形
figure;
plot(t, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Input Signal x(t) in Time Domain');
% 计算DFT
X = fft(x);
f = (0:length(x)-1) * fs / length(x); % 频率向量
% 绘制频谱
figure;
stem(f, abs(X));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('DFT Magnitude Spectrum of x(t)');
% 改变采样时长和补零
T1 = 2; % 增加采样时长
t1 = 0:T:T1-T;
x1 = 10 * cos(2 * pi * 49 * t1) + 15 * cos(2 * pi * 51 * t1);
% 补零
x1_padded = [x1, zeros(1, 1000)];
X1_padded = fft(x1_padded);
f1 = (0:length(x1_padded)-1) * fs / length(x1_padded);
% 绘制频谱
figure;
stem(f1, abs(X1_padded));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('DFT Magnitude Spectrum with Zero Padding');
```
以上代码涵盖了所有实验题目的主要部分,可以根据具体需求进行调整和扩展。希望这些代码对你有所帮助!
阅读全文