How to create a composite signal using MATLAB? [closed]
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this questionIf i have two signals that i generated before, how can i make one composite signal out of them. From my understanding composite signal is to let the two signals appear on the same plot (overlap) so 开发者_如何学Cwhat is the syntax need to prodece that composite signal ??
Note: poth signals have the same time interval.
You need to use the hold
function to retain the first plot while you draw the second one over it. Here's an example:
x=linspace(0,2*pi,1e3);
plot(x,sin(2*x))
hold on
plot(x,cos(2*x),'r')
hold off
hold off
releases the "hold" and if you plot anything now, the previous plots are not retained. It's good practice to release the hold so that you don't inadvertently keep layering multiple plots (unless of course, that's the intention).
If you have two signals of widely ranging amplitudes, one option is to scale them so that they're of comparable amplitudes. This can be achieved for e.g., by normalizing both to a max of 1. E.g.:
x=linspace(0,2*pi,1e3);
y=100*sin(2*x);
z=cos(2*x);
plot(x,y/max(abs(y)),x,z/max(abs(z)))
You might have to make a note or mention (where ever you're using this) that the signals have been normalized so that it is clear.
Alternately you can use the plotyy
function which lets you plot two curves with a y-axis for each. E.g.:
x=linspace(0,2*pi,1e3);
y=100*sin(2*x);
z=cos(2*x);
plotyy(x,y,x,z)
EDIT 2:
To change the colors in plotyy
, you need to use the handles of the lines. Continuing from the above example,
[ax,h1,h2]=plotyy(x,y,x,z);
set(h1,'color','m')
set(h2,'color',[0,0.5,0])
Here I've shown two ways to set the color. One is using in-built color strings ('r'
-> red, 'm'
-> magenta, 'c'
-> cyan, 'g'
-> green, 'b'
-> blue, 'y'
-> yellow, 'k'
-> black, 'w'
-> white) and the other is by a 3 element numeric RGB vector which can take values from 0 to 1.
Sometimes, it is desirable to not have the y-axes colored (I don't like them that way). To change them to black, set the value of the 'ycolor'
property for ax
to black.
set(ax,'ycolor','k')
If I didn't misunderstood, you are looking for something like this:
% x and y (of equal length) represent the samples of two signals
% at the same points in time.
Lx = length(x);
dt = 2; % Interval between samples (inverse of sampling frecuency).
t = (0:Lx-1) * dt; % This will be the horizontal variable.
plot(t, x, 'b;Signal x;', t, y, 'r;Signal y;');
精彩评论