필터 지우기
필터 지우기

Trouble with Implementing Moving Average Filter in MATLAB

조회 수: 24 (최근 30일)
Hemanshu
Hemanshu 2023년 6월 30일
편집: Chunru 2023년 6월 30일
I am encountering difficulties while trying to implement a moving average filter in MATLAB. I have a code snippet that I believe should work, but I am getting unexpected results. Here is the code snippet that I have tried: %Input data inputData = [1, 2, 3, 4, 5, 6, 7, 8, 9]; windowSize = 3; % Moving average filter implementation filteredData = zeros(size(inputData)); for i = 1:length(inputData) startIndex = max(1, i - windowSize); endIndex = min(length(inputData), i + windowSize); filteredData(i) = mean(inputData(startIndex:endIndex)); end

답변 (2개)

Chunru
Chunru 2023년 6월 30일
편집: Chunru 2023년 6월 30일
%Input data
inputData = [1, 2, 3, 4, 5, 6, 7, 8, 9];
windowSize = 3;
% use matlab function
filteredData = movmean(inputData, windowSize)
filteredData = 1×9
1.5000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 8.5000
% Moving average filter implementation
wS = floor((windowSize-1)/2);
filteredData = zeros(size(inputData));
for i = 1:length(inputData)
startIndex = max(1, i - wS);
endIndex = min(length(inputData), i + wS);
filteredData(i) = mean(inputData(startIndex:endIndex));
end
filteredData
filteredData = 1×9
1.5000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 8.5000

Harshavardhan Putta
Harshavardhan Putta 2023년 6월 30일
I understand that you're encountering difficulties while implementing a moving average filter in MATLAB. You can use the movmean function in MATLAB to implement a moving average filter. The movmean function calculates the moving average of a given data sequence using a specified window size.
% Input data
inputData = [1, 2, 3, 4, 5, 6, 7, 8, 9];
windowSize = 3;
% Moving average filter implementation using movmean
filteredData = movmean(inputData, windowSize);
disp(filteredData);
Please refer to the following documentation for more information.
I hope it helps!

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by