1
$\begingroup$

I am trying to program this function: $ g(t)= \operatorname{rect}(t/T) \,f(t)$

I take: $t=0:1:20$ and I know how to program $f(t)$.

But I have a problem with the rectangular function. How to program this function in matlab?

$\endgroup$
1
$\begingroup$
function y = rect(x);

    x = abs(x);

    if x == 0.5
        y = 0.5;
    else
        if  x < 0.5
            y = 1;
        else
            y = 0;
        end
    end

end

put this in a file and name it "rect.m". make sure it's on your MATLAB search path.

$\endgroup$
  • 1
    $\begingroup$ Just check if you like my edit ... $\endgroup$ – Matt L. Aug 11 '18 at 18:21
  • 2
    $\begingroup$ hay @MattL., ever read about the Wicked Blble? (moral of story: proofreading by someone else is necessary sometimes.) $\endgroup$ – robert bristow-johnson Aug 11 '18 at 18:36
  • $\begingroup$ Your original rect.m was indeed pretty wicked ... :) $\endgroup$ – Matt L. Aug 11 '18 at 19:21
1
$\begingroup$

There are a lot of ways

Using an inline function

clear all
x=linspace(-1,1,1024);
rect=@(x) (sign(x+.5)-sign(x-.5))/2
plot(x,rect(x))
title('rect')
xlabel('x')

enter image description here

rect(t/T) is left as an exercise ( give a man a fish and you feed him for a day, teach him how to fish and he will have an excuse to get out of the house)

$\endgroup$
1
$\begingroup$

If you know how to program any function $f(t)$, then you should know how to program a specific function $\mathrm{rect(t/T)}$.

However, Matlab already have a convenient function for that, window.m, which you can pad as desired:

N = 65;
w = window(@rectwin ,N);

But it is more interesting to exercise interesting features in Matlab, including the function handle @ for inline functions, and the function linkaxes.m, to easily test different options (as for discrete signals, different versions for the rectangular functions coexist):

% Choose among four types of discrete rectangular windows
time = (-20:20)'; T = 5;
f = @(t) sin(2*pi*t/30);
nCase = 4;
for iCase = 1:nCase;
    switch iCase
        case 1
            %% Option 1: rectangular is defined as one on ]0,1[
            w = @(t) ((t > 0) & (t < 1));
        case 2
            %% Option 2: rectangular is defined as one  on [0,1]
            w = @(t) ((t >= 0) & (t <= 1));
        case 3
            %% Option 3: rectangular is defined as one  on ]-1/2,1/2[
            w = @(t) (abs(t) < 1/2);
        case 4
            %% Option 4: rectangular is defined as one  on [-1/2,1/2]
            w = @(t) (abs(t) <= 1/2);
    end
    h1(iCase)=subplot(nCase,2,2*(iCase-1)+1) ;
    plot(t,[f(t) w(t/T)],'x-')
    legend('f','w')
    h2(iCase)= subplot(nCase,2,2*(iCase-1)+2);
    plot(t,[f(t).*w(t/T)],'o-')
    legend('f \times w')
    linkaxes([h1,h2],'xy')
end

One result is shown below:

functions and products

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.