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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
设矩形脉冲 是脉冲响应 的LTI系统的输入,求输出 y(n).
5 P& }' C  c( R: q7 Z- N5 M
. x) \! Y- c9 A$ s下面的脚本中用到了一个自定义的函数,也就是两个信号相加的函数:
& m* f: N% a4 ?
& a( Y7 z* o, Q; Zfunction [y,n] = sigadd(x1,n1,x2,n2)8 L2 P( d$ t- }! X$ L
% implements y(n) = x1(n) + x2(n)
3 Y& E& y/ _7 l  G9 X* o3 h% [y,n] = sigadd(x1,n1,x2,n2)
- j$ t+ P" k! v5 f/ }%——————————————————————————————" u, f8 ^/ U, i$ Q6 S4 g9 E: O
% y = sum sequence over n, which includes n1 and n25 h9 R( J  G+ ]* S
% x1 = first sequence over n19 T7 q6 _% H) l3 v: Y. l8 k- R1 N
% x2 = second sequence over n2( n2 can be different from n1)3 T0 K4 v1 ], J
%: T3 D9 Q+ A. j
n = min( min(n1), min(n2) ):max( max(n1), max(n2) ); %duration of y(n)
9 v, C' W! ?) d+ Iy1 = zeros(1,length(n)); y2 = y1; %initialization! ]8 H0 C) E4 |# r) r! ]
y1( find( ( n >= min(n1) )&( n <= max(n1) ) == 1  )  ) = x1; %x1 with duration of y16 ~/ S3 O4 Z8 J8 m( s
y2( find( ( n >= min(n2) )&( n <= max(n2) ) == 1  )  ) = x1; %x2 with duration of y2
  Y" R3 f  a3 S" r2 N! Sy = y1 + y2;& r% p' Z2 d0 b" W

, p1 o! c: k' _2 F  ?7 y9 [3 [: @' Y7 Y0 G$ R4 e. I
直接给出MATLAB脚本:3 M' D! v" v1 x5 {3 A! Q

  d5 K& r1 z( a% M2 Q  Qclc7 `$ ]0 \7 Z+ I* \0 [" U- g2 Z
clear
$ T6 Z7 o, }- W5 ?- k/ Zclose all/ B! t% @; ]! @. n

  Z& {6 v& U. \0 q% help stepseq4 p+ }8 K5 O5 e4 U8 |
%   generate x(n) = u(n - n0); n1 <= n <= n2: {. q$ [# J9 @# g
%  _____________________________________________7 T5 P# r- v4 K+ a
%  [x,n] = stepseq(n0, n1, n2);
! [& s) C# _: ]' }9 R* g, L$ e5 S[u1,n1] = stepseq(0,-5,45);+ @& V& l' f# B+ g
[u2,n2] = stepseq(10,-5,45);- S2 v- M: w# o& |) e
$ v. Q) X6 G* w1 M$ [) S3 B
% generate signal x(n)
7 |3 r$ R/ {% x[x,n] = sigadd(u1,n1,-u2,n2);& y! Z. M4 m2 V3 E% X  E

4 @, N. H9 b. ~- N" G% generate signal h(n)
) y/ W6 C# W3 g, g# Lm = -5:45;
6 `) D; G8 N) W  _h = ( (0.9).^m ).* u1;8 E* T- v- J1 Y: z; T
2 R# O+ c  n6 J" I

& U/ f# K0 n5 F& w$ P3 l+ S1 n% the convolution of x(h) and h(n)
$ g! X7 \; B& X* V# ]# @y = conv(x,h);
: x, L! P6 }4 B- m9 x) S9 {% ensure the index
+ R+ L. K: B: g2 J5 V$ fnyb = n(1)+ m(1);
& e+ M) a2 D5 v* J$ ]nye = n(length(x)) +n(length(h));9 m* P, W) Y3 K9 O7 U2 s; S) ?. y8 m
ny = nyb:nye;+ O( ]1 k, m- _$ |* t1 t
- H$ m7 ~# a6 a7 a& N8 E$ v

, O- P% W- s% @8 M, X" ^' w: `. tsubplot(3,1,1);
  _" W$ }- f% j" Gstem(n,x);
( o0 L+ [7 ?9 }- ititle('x(n)');
" z9 y* _) F  o' Q1 S  zxlabel('n')
# w3 r: |( b2 R6 a/ o( k. E9 b& N5 Z8 b9 t9 J( t5 j) \7 d9 _
subplot(3,1,2);
# _9 H( d& u9 Y; E# V$ Fstem(m,h);
# G' `' e3 l0 e2 E9 c0 }title('h(n)');5 o% e5 e& ^0 m& E, F
xlabel('n')
, l8 U8 E, [" x9 M$ x6 |: q  k$ V: u/ M( J- x" \6 m6 R
subplot(3,1,3);1 U9 r4 E7 s+ S+ z( K5 a" A' W
stem(ny,y);9 v$ p0 a2 E6 c
title('the conv of x(n) and h(n)');! a) m' X! J7 C2 z' t& ^5 F
xlabel('n')( Y# K, `: [  s- n& C
xlim([-5,45]);
* ^  K/ c8 U# o: t+ t$ K0 [9 W& r( l

* j2 N# m6 [( h$ \/ S; B
, q1 j2 F, o+ G5 R
' T4 x+ I# g% B: k3 s4 ~" a9 ]
2 d& I* ^% F2 {5 P! }# X
! f" t/ N5 a5 N( O7 u6 \7 a% s# {! C1 W+ B/ A4 T3 Z- Z3 n- I3 t
# W8 e( G+ Q7 \- B4 [+ \+ ?
  • TA的每日心情
    开心
    2023-5-15 15:14
  • 签到天数: 1 天

    [LV.1]初来乍到

    2#
    发表于 2020-6-1 15:08 | 只看该作者
    subplot这个函数经常使用
    & V1 \& o( X5 s
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

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

    EDA365公众号

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

    GMT+8, 2025-7-24 08:53 , Processed in 0.125000 second(s), 26 queries , Gzip On.

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

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

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