Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Rect Funtion

Subject: Rect Funtion

From: carlos_ray86@hotmail.com

Date: 10 Jun, 2008 20:37:28

Message: 1 of 8

I am trying to create a simple rect function in matlab but not sure
how to do it. My goal is to eventually convolve it with another
argument. The function is f(x)= rect(2x). All it this is, is a
rectangle about the origin that it small. I have been reading around
about doing some sort of vector thing but not sure how it works. So
how would I be able to make a function that is zero everywhere expect
from -1 to +1.

Also I do not know how to create the function Comb(x) which is just an
impulse at 0.

Thanks to all

Subject: Rect Funtion

From: Rick Rosson

Date: 10 Jun, 2008 20:52:33

Message: 2 of 8


For the rect function, please try the following:

    function y = rect(x)


        y = ( (-0.5 <= x) .* (x < 0.5 ) );


    end


HTH.


Rick



<carlos_ray86@hotmail.com> wrote in message
news:bd3323e6-c012-41bb-89b2-0546afd36157@25g2000hsx.googlegroups.com...
>I am trying to create a simple rect function in matlab but not sure
> how to do it. My goal is to eventually convolve it with another
> argument. The function is f(x)= rect(2x). All it this is, is a
> rectangle about the origin that it small. I have been reading around
> about doing some sort of vector thing but not sure how it works. So
> how would I be able to make a function that is zero everywhere expect
> from -1 to +1.
>
> Also I do not know how to create the function Comb(x) which is just an
> impulse at 0.
>
> Thanks to all

Subject: Rect Funtion

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 10 Jun, 2008 20:59:44

Message: 3 of 8

In article <g2mpij$2ne$1@fred.mathworks.com>,
Rick Rosson <no.spam@spammer.abc> wrote:

>For the rect function, please try the following:

> function y = rect(x)
> y = ( (-0.5 <= x) .* (x < 0.5 ) );
> end

Any particular reason, Rick, that you choose .* instead of & ?

Also, the original poster's desired range was -1 to 1 . The poster
did not clearly specify the boundary condition, so I don't know
if that is [-1,1) or (-1,1] or [-1,1] or (-1,-1) .

A version for [-1,1] is

function y = rect(x)
   y = double(abs(x) <= 1)
end
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: Rect Funtion

From: Rick Rosson

Date: 10 Jun, 2008 21:00:53

Message: 4 of 8


For the comb function, please try the following:
    function y = comb(x)


        y = ( x == 0 );


    end

Please note that this function will only work if there is an element that is
exactly equal to 0 in the vector x. If there is a zero-crossing in x where
neither of the two elements on either side of the zero-crossing is exactly
zero, then this function will "miss" the impulse.

You could correct for this problem by trying to detect a change in sign from
one element of x to the next.

HTH.

Rick



<carlos_ray86@hotmail.com> wrote in message
news:bd3323e6-c012-41bb-89b2-0546afd36157@25g2000hsx.googlegroups.com...
>I am trying to create a simple rect function in matlab but not sure
> how to do it. My goal is to eventually convolve it with another
> argument. The function is f(x)= rect(2x). All it this is, is a
> rectangle about the origin that it small. I have been reading around
> about doing some sort of vector thing but not sure how it works. So
> how would I be able to make a function that is zero everywhere expect
> from -1 to +1.
>
> Also I do not know how to create the function Comb(x) which is just an
> impulse at 0.
>
> Thanks to all

Subject: Rect Funtion

From: Rick Rosson

Date: 10 Jun, 2008 21:16:19

Message: 5 of 8

> Any particular reason, Rick, that you choose .* instead of & ?

       No particular reason. Same result either way. I like math better
than logic! ;)


> Also, the original poster's desired range was -1 to 1

      Yes, but he wanted that for y = rect(2*x) not for y = rect(x).
      Also, the "standard" definition for the "rect" or "boxcar"
      function is: y = 1 for abs(x) < 0.5, y = 0 otherwise.
      The main reason is so that the integral of y from -inf to inf
      is then normalized, and the Fourier transform of rect is then
      the standard normal sinc function.

In any case, your implementation might be a bit more efficient than mine.
Depends on efficiency of the abs function versus element-by-element
multiplication.

Thanks for the feedback.

Rick




"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message
news:g2mq00$gga$1@canopus.cc.umanitoba.ca...
> In article <g2mpij$2ne$1@fred.mathworks.com>,
> Rick Rosson <no.spam@spammer.abc> wrote:
>
>>For the rect function, please try the following:
>
>> function y = rect(x)
>> y = ( (-0.5 <= x) .* (x < 0.5 ) );
>> end
>
> Any particular reason, Rick, that you choose .* instead of & ?
>
> Also, the original poster's desired range was -1 to 1 . The poster
> did not clearly specify the boundary condition, so I don't know
> if that is [-1,1) or (-1,1] or [-1,1] or (-1,-1) .
>
> A version for [-1,1] is
>
> function y = rect(x)
> y = double(abs(x) <= 1)
> end
> --
> "Beware of bugs in the above code; I have only proved it correct,
> not tried it." -- Donald Knuth

Subject: Rect Funtion

From: ImageAnalyst

Date: 11 Jun, 2008 01:52:16

Message: 6 of 8

On Jun 10, 4:37=A0pm, carlos_ra...@hotmail.com wrote:
> I am trying to create a simple rect function in matlab but not sure
> how to do it. My goal is to eventually convolve it with another
> argument. The function is f(x)=3D rect(2x). All it this is, is a
> rectangle about the origin that it small. I have been reading around
> about doing some sort of vector thing but not sure how it works. So
> how would I be able to make a function that is zero everywhere expect
> from -1 to +1.
>
> Also I do not know how to create the function Comb(x) which is just an
> impulse at 0.
>
> Thanks to all

----------------------------------------------------------------------------=
-----------
According to terminology that I learned, a Comb function was a series
of Dirac delta functions spaced uniformly apart, while an impulse
function was the "Dirac delta function" or "delta function" for short
(which looks like a comb laying on its backbone with its teeth in the
air). Are you sure your Comb function is just a single delta
function?
Regards,
ImageAnalyst

Subject: Rect Funtion

From: neomis@gmail.com

Date: 12 Jun, 2008 11:58:12

Message: 7 of 8

To do a rect function in matlab the easiest way is just to say:
x=-lower limit : increment : upper limit;
y=abs(x) <= 1;
y = y * amplitude;

comb would be the same thing except
y = abs(x) == 0;

Subject: Rect Funtion

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 12 Jun, 2008 13:52:47

Message: 8 of 8

In article <c88299c7-c27a-4ff0-a1e7-5c23efdbb98e@f63g2000hsf.googlegroups.com>,
 <neomis@gmail.com> wrote:
>To do a rect function in matlab the easiest way is just to say:
>x=-lower limit : increment : upper limit;
>y=abs(x) <= 1;
>y = y * amplitude;

>comb would be the same thing except
>y = abs(x) == 0;

What advantage does y = abs(x) == 0 have over y = x == 0 or y = ~x ?


--
  "Allegories are in the realm of thoughts, what ruins are in
  the realm of things." -- Walter Benjamin

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us