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

5 Ansichten (letzte 30 Tage)
KAE am 15 Apr. 2020
Kommentiert: KAE am 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.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Rik am 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
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
KAE am 15 Apr. 2020
Great idea. This should be in the documentation, I think.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Linear Predictive Coding finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by