# How to fit a common linear trend observed across multiple sensors?

4 views (last 30 days)
KAE on 15 Apr 2020
Commented: KAE on 15 Apr 2020
Let's say I have 10 noisy sensors measuring temperature vs time, and I want to fit a linear trend which is common across all 10 sensors. How do I do this? (I believe I shouldn't average the sensors' values at each time step and then fit a trend to the resulting average, since that doesn't seem to be the same thing, but let me know if it is). Here is an example of the data I want to fit,
%% Make some fake noisy measurements
timeStep = 1:100; % Time step
for iSensor = 1:10 % Loop through sensors
% Dimensions of Temperature: nSensors x nTime
Temperature(iSensor,:) = (5 + rand(1,1))*timeStep + ...% Add noise to the true slope of 5
(rand(1, length(timeStep))-0.5)*100 + 7; % Add noise to the true offset of 7
end
figure;
plot(timeStep, Temperature);
xlabel('Time'); ylabel('Temperature'); title('Noisy Temperature');
All the usual linear regression functions (polyfit, fitlm, regress) seem to assume that Temperature is a vector with dimensions nTime x 1, rather than a matrix of nSensors x nTime.

Rik on 15 Apr 2020
You can just replicate the x-values and linearize all your data:
%% Make some fake noisy measurements
timeStep = 1:100; % Time step
nSensors=10;
Temperature=zeros(nSensors,numel(timeStep));
for iSensor = 1:nSensors % Loop through sensors
% Dimensions of Temperature: nSensors x nTime
Temperature(iSensor,:) = (5 + rand(1,1))*timeStep + ...% Add noise to the true slope of 5
(rand(1, length(timeStep))-0.5)*100 + 7; % Add noise to the true offset of 7
end
figure(1),clf(1)
plot(timeStep, Temperature);
xlabel('Time'); ylabel('Temperature'); title('Noisy Temperature');
timeStep2=ones(size(Temperature)).*timeStep;%lazy repmat
p=polyfit(timeStep2(:),Temperature(:),1);
hold on
plot(timeStep,polyval(p,timeStep),'--k')
hold off
KAE on 15 Apr 2020
Great idea. This should be in the documentation, I think.

### Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by