注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

阿英讲卷积具体过程matlab动态demo  

2013-01-12 22:43:40|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
即以此功德,庄严佛净土。上报四重恩,下济三途苦。惟愿见闻者,悉发菩提心。在世富贵全,往生极乐国。
% startup preparation
close all; clear all; 

%% === PARAMETER SETTING =================================================================
% startIndex4x = 0, endIndex4x = 7. Assume index n=0 is is the first point in vector x
x = [1 2 2 2 -2 -2 -2 1];  %  Input signal, length(x) = 8
% n4t = [0, 1, 2, 3, 4]
h = [0.5, 1, 0, 1, 0.5];  % system inpulse response, length(h) = 5
% discrete time index for y: n4y = [0: 4 + 7], length(y) = 8 + 5 - 1 = 12
% y = conv(x, h) =  0.5 2   3 4 3.5 0 0 -2.5 -2 -3 0 0.5

% -- set discrete time range of convolution axes for full simulation --
nStart = -length(h);  %  discrete time axis begining. At this point, the time reversed h has not slipped into x.
nEnd = length(x) + length(h);  %  discrete time axis end. At this point, the time reversed h has slipped off x completely.
time4conv = nStart:nEnd;  % discrete convolution time axis

% -- embedd x in vector sychronized to discrete convolution time axis --
x4conv = zeros(size(time4conv));  %  Initialize signal vector(x for convoultion show) on sychronized convolution axis with zeros
startIndex4x = find(time4conv == 0);  %  Find index of 0 on discrete convolution time axis
x4conv(startIndex4x:startIndex4x + length(x) - 1) = x;  %  embedd signal x on sychronized convolution axis

% -- embedd h in vector sychronized to discrete convolution time axis --
h4conv = zeros(size(time4conv));
startIndex4h = find(time4conv == 0);  %  Find index of 0 on discrete convolution time axis
h4conv(startIndex4h:startIndex4h + length(h) - 1) = h;

% -- setup the initial position for reversed h which acting as a sliding window --
hReversed4conv = zeros(size(time4conv));   %  Initalize reversed impulse response on sychronized convolution axis with zeros
hReversed4conv(1:length(h)) = fliplr(h);  %  time reversed h 

% -- Initialize output vector --
yout = zeros(size(time4conv));

%*** plot section *********************************************************
%--------------------------------------------------------------
% 1) Plot signal 
%--------------------------------------------------------------
figure(1);
stem(time4conv, x4conv, 'rx');
xlabel('n'); ylabel('x(n)'); set(gcf, 'Position',  [232   457   586   209]);

%--------------------------------------------------------------
% 2) Plot system impulse response 
%--------------------------------------------------------------
figure(2); 
stem(time4conv, h4conv, 'ko');
xlabel('n'); ylabel('h(n)'); set(gcf, 'Position',  [232   457   586   209]);

%--------------------------------------------------------------
% 3) Create figure window for displaying the convolution procedure
%--------------------------------------------------------------
figure(3);
h1 = subplot(2, 1, 1);  %  h1 handles to output window
h2 = subplot(2, 1, 2);  %  Dynamic reversed system impulse response and input window
set(gcf, 'Position', [123   214   720   381]);
%=== END--> PARAMETER SETTING <---END ===========================================================


%% === DYNAMIC CONVOLUTION DEMO, Verion 1 ===================================================================
% version 1: using handle for each subplots to control the behaviors in the loop
%  Loop throught each output sample showing the products between samples to be mulitplied and summed
% hint: the initial state is y(-1), the last state is y(13)
for n = -1:length(x) + length(h)
  %  Find index of current output point
  ind = find(time4conv == n);
  yout(ind) = sum(x4conv.*hReversed4conv); %  Compute product and sum
  stem(h1, time4conv(1:ind), yout(1:ind), 'g>', 'Linewidth', 2);
  axis(h1, [nStart, nEnd,  -4, 4]) ; xlabel(h1, 'n'); ylabel(h1, ['y(', int2str(n), ') = ', num2str(yout(ind))]); 
  title(h1, ['$$ y(n)=  \sum_{m = -1}^{' int2str(length(x) + length(h)) '} x(m)h(n-m) $$'], 'Interpreter', 'Latex');
  
  %  Plot function in convolution sum sychronized with convolution axis
stem(h2, time4conv, x4conv, 'rx', 'Linewidth', 2);
hold on; stem(h2, time4conv, hReversed4conv, 'ko', 'Linewidth', 2); hold off
xlabel(h2, 'm'); title(h2, ['System response h(' int2str(n) '-m) in black O, Signal x(m) in red X'])

  pause;  %  Wait for ket to be hit
  % slide reversed h vector one sample to the right to denote shift from n increment, insert zeros in from the left
  hReversed4conv = [0 hReversed4conv(1:end - 1)];
end
%===END--> DYNAMIC CONVOLUTION DEMO, Version 1 <---END  ===========================================================


%{
%% === DYNAMIC CONVOLUTION DEMO, Verion 2 ===================================================================
% version 2: Abjure the figure handles. Designate figure the specific number explicitly.  I does not know this method is more effective in same case before!
%  Loop throught each output sample showing the products between samples to be mulitplied and summed
% hint: the initial state is y(-1), the last state is y(13)
for n = -1:length(x) + length(h)
  %  Find index of current output point
  ind = find(time4conv == n);
  yout(ind) = sum(x4conv.*hReversed4conv); %  Compute product and sum
  figure(3); subplot(2, 1, 1); stem(time4conv(1:ind), yout(1:ind), 'g>', 'Linewidth', 2);
  axis([nStart, nEnd,  -4, 4]) ; title(['Convolution output up to n = ' int2str(n)]); xlabel('n');
  %hint: The default text units are the units used to plot data in the graph. In text(x, y, 'string') , the origin locates at the center of the current figure
  yLim = get(h1, 'Ylim');  %  get yaxis limits on plot to place text
  text(time4conv(1) + 0.5, yLim(2) - 0.2*(yLim(2) - yLim(1)), ...
      ['$$ y(n)=  \sum_{m=-1}^{' int2str(length(x) + length(h)) '} x(m)h(n-m) $$ ' ],'Interpreter', 'Latex');

%  Plot function in convolution sum sychronized with convolution axis
figure(3); subplot(2, 1, 2); stem(time4conv, x4conv, 'rx', 'Linewidth', 2);
hold on; stem(time4conv, hReversed4conv, 'ko', 'Linewidth', 2); hold off;
xlabel('m'); title(['System response h(' int2str(n) '-m) in black O, Signal x(m) in red X'])
  pause;  %  Wait for ket to be hit
  % slide reversed h vector one sample to the right to denote shift from n increment, insert zeros in from the left
  hReversed4conv = [0 hReversed4conv(1:end - 1)];
end
%===END--> DYNAMIC CONVOLUTION DEMO, Version 2 <---END  ===========================================================
%}

阿英讲卷积具体过程matlab动态demo - 阿英 - Mr.Right
 
  评论这张
 
阅读(669)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016