找回密码
 注册
关于网站域名变更的通知
查看: 445|回复: 1
打印 上一主题 下一主题

两个序列的卷积和运算的MATLAB实现(1)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-6-1 14:09 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
设矩形脉冲 是脉冲响应 的LTI系统的输入,求输出 y(n).
" N  b$ V: w: O! H- M7 t5 W+ V  k* c+ T5 o# J( o5 h! K: C
下面的脚本中用到了一个自定义的函数,也就是两个信号相加的函数:! N  f: K! Q* \8 I3 k- s2 t

! j- m7 j+ w7 C0 n+ Xfunction [y,n] = sigadd(x1,n1,x2,n2)
0 q* m7 {2 B) A$ P1 m. [# E% implements y(n) = x1(n) + x2(n): y5 U3 a! o6 U. a. d, `
% [y,n] = sigadd(x1,n1,x2,n2)# I, E" ]  K8 {& k  N& \4 ]' }
%——————————————————————————————
$ M* ?4 }$ j* d; c/ a6 m# `" L% y = sum sequence over n, which includes n1 and n2) X) g! h; `. W
% x1 = first sequence over n1
" k, A/ e3 M+ Q5 H" @) V" J% x2 = second sequence over n2( n2 can be different from n1)
1 ~( C* d' u, R; N" h5 j% {3 e$ a%
6 S* J+ T( M3 f$ ~n = min( min(n1), min(n2) ):max( max(n1), max(n2) ); %duration of y(n)
1 p; v3 E. A$ [, m4 \y1 = zeros(1,length(n)); y2 = y1; %initialization
" \4 g9 e/ F/ `4 j3 J- ~3 [y1( find( ( n >= min(n1) )&( n <= max(n1) ) == 1  )  ) = x1; %x1 with duration of y15 q, p8 y( K# K* R0 g. p. D' e
y2( find( ( n >= min(n2) )&( n <= max(n2) ) == 1  )  ) = x1; %x2 with duration of y26 @( J/ N) P+ v3 U+ a3 l' M
y = y1 + y2;  p1 n6 ^( t& g* M

: n2 V9 A8 X7 ]* z3 o2 |
( X) d) X! j* q: d$ ~% R7 g直接给出MATLAB脚本:7 V# C& z+ x# S+ y0 H
7 {6 L0 Y+ G6 Q3 f
clc- _0 j- P* o! Z8 o- m6 p
clear
4 m  Y: I) r5 A+ M, g* Z! Zclose all
+ N/ R* J+ o0 m1 I/ T8 K3 r+ w3 R: Y, O7 ^! ^
% help stepseq5 A8 q) d/ p0 S0 m6 C, F6 L, O
%   generate x(n) = u(n - n0); n1 <= n <= n26 c9 n, ]. Y3 L1 A2 e
%  _____________________________________________, B- F% q8 r9 k% g
%  [x,n] = stepseq(n0, n1, n2);
) A, i$ {6 J3 v5 z- z& A& _/ y8 l[u1,n1] = stepseq(0,-5,45);
( R2 N, x9 G# M- p[u2,n2] = stepseq(10,-5,45);
- z( D- m( r7 L0 M2 r! P1 W9 S- O; _' \% l6 V4 d6 j1 N+ j1 ?/ z
% generate signal x(n)- o3 K. c( I! P" d0 m# f
[x,n] = sigadd(u1,n1,-u2,n2);0 h0 C& K& I2 H- i* Q

) T7 i4 ^& O/ C  }) k3 I4 l% generate signal h(n)
: n9 o3 o7 I; W9 H: ?9 Km = -5:45;
3 F' R0 j8 d: s% ^; Th = ( (0.9).^m ).* u1;* b2 _. z: c1 ^7 F5 |

6 ]( B/ ]# Y! W: f) w" F; T8 C# u" h0 u: f2 h) M6 Z0 y
% the convolution of x(h) and h(n)
; H3 I) Y$ b; F! Zy = conv(x,h);7 d; w" D  ~7 A( H
% ensure the index& `. M. |9 r: u( e7 W, {8 \. x
nyb = n(1)+ m(1);1 K. ]: r9 }2 U
nye = n(length(x)) +n(length(h));8 E: A) a' O' e  H, n
ny = nyb:nye;
  s* m# h8 x. r& Q7 _; e8 T  V) M- t7 n1 s  G! J
: H( i. {! J+ t* e+ I* P* H
subplot(3,1,1);& {/ b$ R8 m- l" [
stem(n,x);
/ E6 P6 H! U9 U; ~  {title('x(n)');$ K% D- y) X# o/ L  D9 w
xlabel('n')
) U7 p  b  w9 r# |# h. K$ e. N5 D& w$ [; f
subplot(3,1,2);" l) |; W6 S8 r% r# e+ I% `
stem(m,h);% X9 Z3 V0 ]3 |: I6 T- E" M0 e
title('h(n)');
) k3 p6 _8 Q6 S1 l! Bxlabel('n')
  t* d3 {# v* h! z4 \2 S
6 o9 K& \6 X  X5 Y  x% f  p" Q- Ysubplot(3,1,3);& I# W& t% k7 I  e+ ], P7 N. S" R: D
stem(ny,y);8 G9 H, _! \1 x! m" s9 }. h8 f( b
title('the conv of x(n) and h(n)');8 h5 \- G4 h& s$ _7 K7 G
xlabel('n')
& c* U' F- c/ C: x0 }! mxlim([-5,45]);: n$ D% X" p8 A  D4 ]

- a$ k/ L# D3 W5 N3 ~+ V1 A+ W' N2 g0 v3 m% v+ ~

9 k; I$ `( w/ `
% d# N- X" D9 {8 A2 E) V7 q& w$ [6 L+ q9 A

& I5 \" X9 ^3 u2 R5 e& \1 O5 q& h: L+ |: q0 C9 p
  i: O; r/ R! \6 V7 X
  • TA的每日心情
    开心
    2023-5-15 15:14
  • 签到天数: 1 天

    [LV.1]初来乍到

    2#
    发表于 2020-6-1 15:08 | 只看该作者
    subplot这个函数经常使用
    3 @+ _% O3 k- ~1 v+ J  N7 h
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-24 12:22 , Processed in 0.156250 second(s), 27 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表