calculating the costs of a change in mean using the standard deviation

1 次查看(过去 30 天)
I have a signal that I am trying to find change points in using the sliding window method. I have computed the mean in windows (from a previous answer on the forum) and I will like to calculate the costs of a change in mean using the standard deviation so as to detect points with highest costs.
Basically how do i add a cost function to the function below.
function Output = SlideFun(x,WindowLength)
Output = zeros(length(x)-WindowLength,1);
for idx = 1:length(x)-WindowLength
Block = x(idx:idx+WindowLength);
Output(idx) = mean(Block);
end
end

回答(1 个)

Satyam
Satyam 2025-6-10
Hi Daniel,
To detect change points in a signal using the sliding window method, the function 'SlideFun' can be extended by incorporating a cost function that measures the change in mean relative to the standard deviation within each window.
In this approach, for each window of the signal, the data is split into two halves. The mean of each half is computed, and the absolute difference between these means is calculated. This difference is then normalized by the standard deviation of the full window, producing a cost value that reflects the likelihood of a change point at that position.
The modified function returns this cost instead of the raw mean, allowing for the identification of regions with statistically significant shifts. By analyzing the resulting cost values, typically through peak detection, change points in the signal can be effectively identified. You can leverage 'findpeaks' function of MATLAB to locate local maximas in the cost values. Refer to the documentation to learn about the syntax of 'findpeaks' function: http://mathworks.com/help/signal/ref/findpeaks.html
Below is a sample code completing the 'SlideFun' function by incorporating cost functionality:
% Generate dummy signal with changes in mean
x = [ones(1, 50)*2, ones(1, 50)*5, ones(1, 50)*1.5] + randn(1, 150)*0.3;
WindowLength = 20;
Cost = SlideFun(x, WindowLength);
[peaks, locs] = findpeaks(Cost, 'MinPeakHeight', 0.99); % Threshold adjustable
% Plot results
figure;
% Original signal
subplot(2,1,1);
plot(x, 'LineWidth', 1.5);
hold on;
plot(locs + round(WindowLength/2), x(locs + round(WindowLength/2)), 'ro', 'MarkerSize', 8, 'LineWidth', 2);
title('Original Signal with Detected Change Points');
xlabel('Index'); ylabel('Amplitude');
legend('Signal', 'Change Point');
% Cost function
subplot(2,1,2);
plot(Cost, 'k', 'LineWidth', 1.5);
hold on;
plot(locs, peaks, 'r*', 'MarkerSize', 8);
title('Sliding Window Cost Function');
xlabel('Index'); ylabel('Cost');
legend('Cost', 'Change Point');
function Cost = SlideFun(x, WindowLength)
NumPoints = length(x) - WindowLength;
Cost = zeros(NumPoints, 1);
for idx = 1:NumPoints
Block = x(idx : idx + WindowLength);
Half = floor(WindowLength / 2);
Block1 = Block(1:Half);
Block2 = Block(Half+1:end);
Mean1 = mean(Block1);
Mean2 = mean(Block2);
StdAll = std(Block);
if StdAll == 0
Cost(idx) = 0;
else
Cost(idx) = abs(Mean2 - Mean1) / StdAll;
end
end
end
I hope it solves the query.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by