matlab 实现信号的微分和积分

1.使用中值法对信号进行微分处理

在这里插入图片描述

1.1 正弦信号的微分处理

%生成正弦波信号
Fs=500;%采样频率是500Hz
T=1;%时间是1秒
dt=1.0/Fs;%step等于 1/500
N=T/dt;%数组的个数为N
t=linspace(0, T, N);%生成一个数组[0, 0.002, 0.004, 0.006,...,0.998, 1.000]
y=10*sin(2*pi* 5 *t);%生成频率为5Hz,幅值为10,时间为1秒的正弦波
subplot(2, 1, 1);
plot(t, y);

%给正弦波形信号做微分处理得到的是余弦波形
y1=y;
y1(1)=y(1);
for k=2:1:N-1
    y1(k)=(y(k+1) - y(k-1))/(2*dt);
end
y1(N)=y(N);
subplot(2, 1, 2);
plot(t, y1);

上图是正弦信号波形,下图是对其进行微分的波形
在这里插入图片描述

1.2 方波信号的微分处理

t=0:0.001:50;
f=square(t);
subplot(2, 1, 1);
plot(t, f);%画出以数组t为横坐标,数组f为纵坐标组成的点,并用线将这些点连起来
axis([-1,51, -2, 3]);%设置X坐标的显示范围为[-1,11],Y坐标的显示范围为[-1,3],注意只有写在plot函数之后才会生效

%给方波形信号做微分处理
y=f;
dt=0.001;
y(1)=f(1);
for k=2:1:(50.0/0.001)
    y(k)=(f(k+1) - f(k-1))/(2*dt);
end
y(50.0/0.001)=f(50.0/0.001);
subplot(2, 1, 2);
plot(t, y);

在这里插入图片描述

1.3 锯齿波信号的微分处理

t=0:0.001:10;
f=sawtooth(2*pi* 1 *t + (pi/2));
subplot(2,1,1);
plot(t,f,'r','LineWidth',3);
grid on;
axis([-0.5,10.5, -1.5,1.5]);

%给锯齿波形信号做微分处理
y=f;
dt=0.001;
y(1)=f(1);
for k=2:1:(10.0/0.001)
    y(k)=(f(k+1) - f(k-1))/(2*dt);
end
y(10.0/0.001)=f(10.0/0.001);
subplot(2, 1, 2);
plot(t, y);

在这里插入图片描述

1.4 三角波信号的微分处理

t=0:0.001:10;
f=sawtooth(2*pi* 1 *t + (pi/2), 1/2);
subplot(2, 1, 1);
plot(t,f,'r','LineWidth',3);
grid on;
axis([-0.5,10.5, -1.5,1.5]);

%给三角波形信号做微分处理
y=f;
dt=0.001;
y(1)=f(1);
for k=2:1:(10.0/0.001)
    y(k)=(f(k+1) - f(k-1))/(2*dt);
end
y(10.0/0.001)=f(10.0/0.001);
subplot(2, 1, 2);
plot(t, y);

在这里插入图片描述

2.使用梯形法给信号做数字积分

在这里插入图片描述

2.1 三角波信号的积分处理

t=0:0.001:10;
f=sawtooth(2*pi* 1 *t + (pi/2), 1/2);
subplot(2, 1, 1);
plot(t,f,'r','LineWidth',3);
grid on;
axis([-0.5,10.5, -1.5,1.5]);

%给三角波形信号做积分处理
y=f;
dt=0.001;
y(1)=0;
for k=2:1:(10.0/0.001)
    y(k)=y(k-1) + (dt*(f(k)+f(k-1)))/2;
end
subplot(2, 1, 2);
plot(t, y);

在这里插入图片描述

2.2 方波形信号的积分处理

t=0:0.001:50;
f=square(t);
subplot(2, 1, 1);
plot(t, f);%画出以数组t为横坐标,数组f为纵坐标组成的点,并用线将这些点连起来
axis([-1,51, -2, 3]);%设置X坐标的显示范围为[-1,11],Y坐标的显示范围为[-1,3],注意只有写在plot函数之后才会生效

%给方波形信号做积分处理
y=f;
dt=0.001;
y(1)=0;
for k=2:1:(50.0/0.001)
    y(k)=y(k-1) + (dt*(f(k)+f(k-1)))/2;
end
subplot(2, 1, 2);
plot(t, y);

在这里插入图片描述

3. 给三角波信号先做微分处理,再做积分处理

t=0:0.001:10;
f=sawtooth(2*pi* 1 *t + (pi/2), 1/2);
subplot(3, 1, 1);
plot(t,f,'r');
grid on;

%给三角波形信号做微分处理得到方波信号
y=f;
dt=0.001;
y(1)=f(1);
for k=2:1:(10.0/0.001)
    y(k)=(f(k+1) - f(k-1))/(2*dt);
end
y(10.0/0.001)=f(10.0/0.001);
subplot(3, 1, 2);
plot(t, y);

%再给上面的方波做积分处理
y1=y;
dt=0.001;
y1(1)=0;
for k=2:1:(10.0/0.001)
    y1(k)=y1(k-1) + (dt*(y(k)+y(k-1)))/2;
end
subplot(3, 1, 3);
plot(t, y1);
axis([0,10, -1,1]);
grid on;

在这里插入图片描述
现在我们利用定时器,让三角波信号动起来,动态观察三角波信号的微分和积分。

在这里插入图片描述

关键代码如下:

global chushixiangwei
chushixiangwei=0;
global mytimer
mytimer=timer('Period',1,'ExecutionMode','fixedDelay');
mytimer.TimerFcn={@my_callback_fcn};

function my_callback_fcn(hObject, eventdata, handles)
    %生成三角波信号
    t=0:0.001:10;
    global chushixiangwei
    f=sawtooth(2*pi* 1 *t + (pi/2) + chushixiangwei, 1/2);
    chushixiangwei=chushixiangwei+0.5;
    subplot(3, 1, 1);
    plot(t,f,'r','LineWidth',3);
    grid on;
    
    
    %给三角波形信号做微分处理
    y=f;
    dt=0.001;
    y(1)=f(1);
    for k=2:1:(10.0/0.001)
        y(k)=(f(k+1) - f(k-1))/(2*dt);
    end
    y(10.0/0.001)=f(10.0/0.001);
    subplot(3, 1, 2);
    plot(t, y);

    %再做积分处理
    y1=y;
    dt=0.001;
    y1(1)=0;
    for k=2:1:(10.0/0.001)
        y1(k)=y1(k-1) + (dt*(y(k)+y(k-1)))/2;
    end
    subplot(3, 1, 3);
    plot(t, y1);
    axis([0,10, -1,1]);
    grid on;

function pushbutton1_Callback(hObject, eventdata, handles)
    %先要找到所有的定时器,让它们先停下来
    ts=timerfind; 
    if length(ts)>0 
       stop(ts); 
    end 
    %然后开启定时器
    global mytimer;
    start(mytimer);


function pushbutton2_Callback(hObject, eventdata, handles)
    ts=timerfind; 
    if length(ts)>0 
       stop(ts); 
       delete(ts); 
     end 

  • 4
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值