Moving Average using MATLAB : Application for 1D data




Introduction

An effective statistical method for discovering underlying trends in time series data is the moving average. Moving averages are frequently used in data analysis. By "moving" the window along the series, it determines the average value of a portion of successive data points.

Benefits of using moving average:

  1. Smoothing: By removing noise and fluctuations from the data, moving averages make it simpler to spot underlying trends or patterns. It assists in reducing transient anomalies and exposing long-term patterns.
  2. Trend identification: Moving average enables us to determine the overall direction or trend of the data by calculating the average over a predetermined time frame. It aids in determining if the data is rising, falling, or varying near a constant mean.
  3.  Noise reduction: The effect of outliers or unpredictable variations in the data is lessened by moving average. It provides a more stable representation of the underlying signal by averaging out the effect of individual data points.

Let's Practice ...!

Here's a simple MATLAB script (.m file) that calculates the moving average of a 1D matrix:

function output = movingAverage(input, windowSize)
output = movmean(input, windowSize);
end

In this script, the movmean function is used to calculate the moving average of the input data. The input parameter represents the 1D matrix, and the windowSize parameter determines the size of the moving window (i.e., the number of consecutive data points to consider for averaging).

To use this script, you can call the movingAverage function and provide your input data and desired window size. For example:

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
windowSize = 3;

result = movmean(data, windowSize);
disp(result);

(Note: just copy and paste the scripts above to your Matlab Command Windows to practice using it and see the result)

In the above example, the input data is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], and the window size is set to 3. The output will be [2, 3, 4, 5, 6, 7, 8, 9], which represents the moving average calculated for each window of size 3.

Feel free to modify the code according to your specific requirements, such as changing the input data or window size, or incorporating additional calculations or operations.

To provide a concrete example of the application of moving average on 1D data, let's consider a hypothetical scenario where we have daily temperature data for a year. We'll generate a random dataset to demonstrate the smoothing effect of the moving average.

% Generate random temperature data for a year (365 days)
rng(1); % Set random seed for reproducibility
temperatureData = randn(1, 365) * 10 + 25;

% Calculate the moving average with a window size of 7
windowSize = 7;
movingAvg = movmean(temperatureData, windowSize);

% Plot the original data (in blue) and the moving average (in red)
figure;
plot(temperatureData, 'b', 'LineWidth', 1.5);
hold on;
plot(movingAvg, 'r', 'LineWidth', 2);
hold off;
xlabel('Day');
ylabel('Temperature');
title('Moving Average of Daily Temperature Data');
legend('Original Data', 'Moving Average');

when you're running it, you will get the result like this:


In this example, we generate a random temperature dataset for 365 days using randn, which creates normally distributed random numbers. The data is then smoothed using a moving average with a window size of 7, representing a weekly average.

The resulting plot displays the original temperature data in blue and the moving average in red. The moving average line appears smoother compared to the original data. By taking the average over a window of 7 consecutive days, the short-term fluctuations are reduced, and the overall trend becomes more evident.

The benefit of using moving average in this context becomes apparent as it helps in visualizing the long-term trend of the temperature data while suppressing day-to-day variations or noise. It provides a clearer understanding of the temperature patterns over time, allowing us to identify any underlying trends or seasonality.

By smoothing out the data, the moving average enables us to focus on the larger patterns or changes in temperature, such as gradual increases or decreases, seasonal trends, or anomalies that persist over multiple days.

Feel free to adjust the window size or experiment with different datasets to observe the impact of the moving average on different time series data.


Find other articles in the topic or label of data analytics to improve your knowledge and skill in analyzing your data for any purposes. InsyaAllah, I guarantee you'll learn much about it here. have a good day!

If you have any further questions or need assistance with data analysis or MATLAB programming, feel free to ask!


-asb, founder of myresearchxpress


#data analysis with matlab, #data science with matlab, #data analytics with matlab,

#data visualization with matlab, #big data with matlab, #data processing with matlab,

#plot data with matlab, #data acquisition with matlab, #data science with matlab pdf




myresearchxpress

Hi, i"m asep sandra, a researcher at BRIN Indonesia. I want to share all about data analysis and tools with you. Hopefully this blog will fulfill your needs.

Posting Komentar

Lebih baru Lebih lama