Igor Pejic
Igor Pejic

Reputation: 3698

How to generate signal with negative values?

I have to generate a discrete signal which is defined as:

1, -3<= n<=3
and 0 otherwise

Since matlab doesn't accept negative indexes of arrays how do I do it? I imagined it was periodic and did this:

n=0:7;
x1 = [1 1 1 1 0 1 1 1];

Is this correct? The problem is I have to make a Fourier transform after that.

Upvotes: 0

Views: 539

Answers (2)

fhdrsdg
fhdrsdg

Reputation: 10532

You shouldn't use your x-vector's index as n-value, since that limits you to positive integers only. Create a separate n-vector with the corresponding n-values:

n = -10:10;
x = zeros(size(n));
x(n>=-3 & n<=3) = 1;
plot(n,x)

Or with smaller steps for n:

n = -10:0.1:10;
x = zeros(size(n));
x(n>=-3 & n<=3) = 1;
plot(n,x)

Upvotes: 2

Daniel
Daniel

Reputation: 36710

Use a function handle:

y=@(n)(-3<n & n<=3)

And to plot it:

x=-7:7
stem(x,y(x))

Upvotes: 3

Related Questions