%矩形波尺度变换 clc; clear; close all; t = -4:0.01:4; T = 1; f = rectpuls(t, T); ft1 = rectpuls(4*t, T); ft2 = rectpuls(1/4*t, T); subplot(3, 1, 1); plot(t, f); title('f(t)'); axis([-4 4 -0.5 1.5]); subplot(3, 1, 2); plot(t, ft1); title('f(4t)'); axis([-4 4 -0.5 1.5]);
时间: 2024-04-01 18:34:46 浏览: 10
subplot(3, 1, 3); plot(t, ft2); title('f(1/4t)'); axis([-4 4 -0.5 1.5]);
这段代码是用 MATLAB 实现矩形波的尺度变换。首先定义了时间轴 t,矩形波的周期 T,以及原始的矩形波 f(t)。然后又定义了两个新的矩形波,它们的周期分别是原始矩形波的 1/4 和 4 倍。最后用 subplot 函数将三个矩形波画在同一个图像窗口中,方便比较它们的变化。
相关问题
clear clc t= -100:0.001:100; % 初值: 增量: 终 值 syms x; y = x/(x * x + 1); f = inline(y); % 内联函数 max = max(f(t)) min = min(f(t))
代码解释:
- `clear` 和 `clc` 分别是清空变量和清空命令窗口的指令。
- `t = -100:0.001:100` 定义了一个从 -100 到 100 的数组,步长为 0.001,用于后面计算函数的取值。
- `syms x` 定义了一个符号变量 x。
- `y = x/(x * x + 1)` 定义了一个函数表达式,表示 y = x / (x^2 + 1)。
- `f = inline(y)` 将函数表达式转换为一个可调用的函数 f。
- `max = max(f(t))` 计算函数 f 在 t 中的最大值。
- `min = min(f(t))` 计算函数 f 在 t 中的最小值。
完整代码如下:
```matlab
clear
clc
t = -100:0.001:100;
syms x;
y = x/(x * x + 1);
f = inline(y);
max = max(f(t));
min = min(f(t));
```
%一阶声波方程模拟 clear;clc; %雷克子波 % figure(1); dt=1e-3; tmax=501; t=0:d
tmax=dt:(tmax-1)*dt; %时间范围
f1=10; %第一个子波的频率
f2=20; %第二个子波的频率
t1=1/f1; %第一个子波的周期
t2=1/f2; %第二个子波的周期
a1=2; %第一个子波的振幅
a2=1; %第二个子波的振幅
w=pi/(sqrt(t1^2+t2^2)); %角频率
delta=t1*t2/(t1+t2); %相位差
t=t-tmax/2*dt; %时间向左平移
q=a1*sin(w*t).*exp(-((t-tmax/(2*dt))/t1).^2)+a2*sin(w*t+delta).*exp(-((t-tmax/(2*dt))/t2).^2); %构造雷克子波
figure; %绘制雷克子波图像
plot(t,q);
xlabel('时间(s)');
ylabel('振幅');
title('雷克子波');
figure; %绘制频谱图
N=length(q); %信号长度
df=1/(N*dt); %频率分辨率
f=linspace(0,1/(2*dt),N/2+1); %频率范围
Q=fft(q,N)/N; %信号的傅里叶变换
Q=2*abs(Q(1:N/2+1)); %归一化并取幅值
plot(f,Q);
xlabel('频率(Hz)');
ylabel('幅值');
title('雷克子波频谱');
figure; %使用一阶声波方程模拟
c=1500; %声速
dx=0.01; %网格间距
dt2=0.5*dx/c; %计算时间间隔
tmax2=max(t)+100*dt; %计算模拟时间
nx=round(max(tmax2*c/dx,2/tmax2/dt2)); %计算网格数
x=0:dx:(nx-1)*dx; %空间范围
P=zeros(nx,1); %初始化压力场
P(2:nx-1)=q(1:nx-2)/2*q(2:nx-1)/2; %初始脉冲赋值
for t2=0:dt2:tmax2 %迭代计算
P(2:nx-1)=P(2:nx-1)+(c*dt2/dx*(P(3:nx)-P(2:nx-1))); %更新压力场
P(1)=0; P(nx)=0; %边界条件
if mod(t2,dt)==0 %每个时间步长绘制结果
figure;
plot(x,P);
xlabel('距离(m)');
ylabel('幅值');
title(['声波传播 t=',num2str(t2)]);
end
end