MATLAB: Mean and Root Mean Square plot

meanrmsvectors

I wrote the following code. I have one question regarding mean and SMS. Since the Disp_Data is a vecotor, how can I write the code to make sure the mean and RMS result come as a vector as well for plotting.
Time = 0:0.001:5;
T = Time(2)-Time(1); % Sampling period
Fs = 1/T; % Sampling frequency
L = size(Time,2); % Length of signal
Disp_Data = 5*(sin(10*2*pi*Time)+0.1*((rand(1,L)-0.5)*2));
M_data = mean(Disp_Data);
RMS_data = RMS (Disp_Data)

Best Answer

Try this:
Time = 0:0.001:5;
T = Time(2)-Time(1); % Sampling period
Fs = 1/T; % Sampling frequency
L = numel(Time); % Length of signal
Disp_Data = 5*(sin(10*2*pi*Time)+0.1*((rand(1,L)-0.5)*2));
M_data = mean(Disp_Data);
RMS_data = rms(Disp_Data);
figure
plot(Time, Disp_Data)
hold on
plot(xlim, [1 1]*M_data, 'LineWidth',1.5)
plot(xlim, [1 1]*RMS_data, 'LineWidth',1.5)
hold off
grid
xlabel('Time')
ylabel('Amplitude')
legend('Signal', 'Signal Mean', 'Signal RMS')
Note that the mean and RMS values are the same for the entire signal.