Alex Encore
Alex Encore

Reputation: 309

MATLAB: Plot random signal

My task is to plot a completely random signal.

This is my progress so far:

sig_length = 200; % this task is part of a three plot figure so ignore the subplot part
subplot(2,2,1)
hold on
sig = rand(1,sig_length);
plot(1:sig_length,sig)
axis tight
title('Signal')

My problem is that I want the interval of the y axis to be -5 to 5. How can I achieve that? Thank you in advance.

Upvotes: 0

Views: 7275

Answers (2)

Cory Dolphin
Cory Dolphin

Reputation: 2670

  1. In order to set the axis, use axis([xmin xmax ymin ymax]) more documentation can be found http://www.mathworks.com/help/techdoc/ref/axis.html

  2. In order to create a signal that is centered about 0, with a uniform distribution on the open interval -5 to 5, you must first scale rand by a factor of 10 (rand produces values on the open interval (0,1), and you need values on the range (-5,5)), and shift it up by 5, as seen below:

    shiftedCenteredSig = (10*rand(1,sig_length)) - 5 %scaled and shifted to be from -5 to 5

This pattern/recipe can be seen in the examples in the documentation: http://www.mathworks.com/help/techdoc/ref/rand.html:

Examples Example 1 Generate values from the uniform distribution on the interval [a, b]:

r = a + (b-a).*rand(100,1);

The final code can be seen below:

sig_length = 200; % this task is part of a three plot figure so ignore the subplot part
subplot(2,2,1)
hold on
%sig = rand(1,sig_length); %note this is a uniform distribution on interval 0,1
sig = (10*rand(1,sig_length)) - 5 %scaled and shifted to be from -5 to 5
plot(1:sig_length,sig)
axis([1,sig_length,-5,5])
title('Signal')

Upvotes: 2

Phonon
Phonon

Reputation: 12737

If you want your signal to go from -5 to 5,

sig = -5 + 10*rand(1,sig_length);

In general, for a random signal to go between a and b, use

a + (b-a)*rand(1,length);

Upvotes: 3

Related Questions