Hi all, I am writing a user defined convolution program in matlab. I have some indexing problems ...please correct and help me with my code: function Out = userconv(a,b) L =length(a);%input M=length(b); %filter t=0; c=zeros(1,L+M-1); % initialize the output data buffer w=zeros(1,M); for i=1 : L+M-1 c(i)=0; for j=1 :M if (i>j) && if ( i-j)<L c(i)=c(i)+b(i-j) end end Thanks , Robert.
convolution in matlab
Started by ●August 5, 2003
Reply by ●August 5, 20032003-08-05
"robert_dn" <robert_dn2002@yahoo.com> wrote in message news:3c74b7a.0308051001.77cc97c4@posting.google.com...> Hi all, > I am writing a user defined convolution program in matlab. I have some > indexing problems ...please correct and help me with my code: > > function Out = userconv(a,b) > > L =length(a);%input > M=length(b); %filter > t=0; > c=zeros(1,L+M-1); % initialize the output data buffer > w=zeros(1,M); > > for i=1 : L+M-1 > c(i)=0; > for j=1 :M > if (i>j) && if ( i-j)<L > c(i)=c(i)+b(i-j) > end > endyour "w" isn't used anywhere. nor t. a is an important input but isn't used anywhere after finding its length. I wouldn't use conditions on the indices as you've done - rather make the indices what you intend them to be. Maybe have conditional code segments. Doesn't Out have to be assigned a value now and then? Fred
Reply by ●August 6, 20032003-08-06
robert_dn2002@yahoo.com (robert_dn) wrote in message news:<3c74b7a.0308051001.77cc97c4@posting.google.com>...> Hi all, > I am writing a user defined convolution program in matlab. I have some > indexing problems ...please correct and help me with my code: > > function Out = userconv(a,b) > > L =length(a);%input > M=length(b); %filter > t=0; > c=zeros(1,L+M-1); % initialize the output data buffer > w=zeros(1,M); > > for i=1 : L+M-1 > c(i)=0; > for j=1 :M > if (i>j) && if ( i-j)<L > c(i)=c(i)+b(i-j) > end > end > > Thanks , > > Robert.Please define your UserConv. 1. Your if statement, U need a single &, 2. Same statement another "if"?? 3. b(i-j) ... MISTAKE... size of b is limited to M you will be having an indexing problem if the i-j > M.
Reply by ●August 11, 20032003-08-11
The procedure for computing the convolution is wrong, you are trying to convolute a and b. In your program the sequence a does not come into picture any where in the code. The convolution of any 2 sequences is given by c(n)=sum (k= -inf to k= +inf) { x(k) * h(n-k)} in your code assume a to be x(n) and b to be h(n), length of a and b be m,n respectively then length of the convoluted sequence is n+m-1; So modify your code c(i)=c(i)+ b(i) * a(i-j); and put a condition to check h(i-j) and b(i) is defined or not, this should work.
Reply by ●February 3, 20092009-02-03
hi.. here i m sending correct convolution code for matlab...%************************************************************************* % Convolution of Two signals %************************************************************************** %DKV clc; close all; clear all; x=[1,2,3,4]; %first signal h=[1,2,3,4,5]; %second signal N1=length(x); N2=length(h); X=[x,zeros(1,N2)];% padding of N2 zeros H=[h,zeros(1,N1)];% padding of N1 zeros for i=1:N1+N2-1 y(i)=0; for j=1:N1 if(i-j+1>0) y(i)=y(i)+X(j)*H(i-j+1); else end end end stem(y); ylabel('y[n]'); xlabel('----->n'); title('Convolution of Two Signal');