A simple moving point average in Matlab

A simple  moving point average in MATLAB.

A moving average or a rolling average is one of the most common smoothing technique used to extract a good signal out of a very random noisy signal. This technique is usually used to see the behavior of a function or  a signal, when the physical parameters and environment have an erroneous effect on the measured signal.

Given a series of numbers and a fixed subset size, the first element of the moving average is obtained by taking the average of the initial fixed subset of the number series. Then the subset is modified by “shifting forward”; that is, excluding the first number of the series and including the next number following the original subset in the series. This creates a new subset of numbers, which is averaged. This process is repeated over the entire data series. The plot line connecting all the (fixed) averages is the moving average. A moving average is a set of numbers, each of which is the average of the corresponding subset of a larger set of datum points. A moving average may also use unequal weights for each datum value in the subset to emphasize particular values in the subset.

The general technique involves finding the mean  from an equal number of data on either side of a central value. This ensures that variations in the mean are aligned with the variations in the data rather than being shifted in time. There can be some anomalies when the variation is not uniform as well, but this will not be discussed here.

There can be different types of moving point averages like  the

  • Cumulative moving average
  • Weighted moving average
  • Exponential moving average
  • Modified moving average, and
  •  Regression  moving average methods

In this post I have attached a MATLAB code to do a simple moving average. This code can be used to smooth a signal with some nice feature but with a small background noise without compromising on the data value. But be careful on the window span of the average for your own data.

Find the code here:




Multi-curve fitting in Origin Pro.

Multi-curve fitting in Origin Pro.

Origin is a data processing and analyzing software and probably one of the most popular software choice to analyse data in industry,academia and laboratories. The versatility of the software is its easy to use, updates in real time, robust analysis, import and export capabilities even to LATEX. I will recommend this software to anyone who spends lot of time crunching numbers and getting some sweet results from it.

Yes, Matlab can do curve fit as well using cftool option. Yet there are some functions which can get a bit complicated with matlab fit tool, and also the energy to write a code when you can use ready to use  functions( in thousands) to do that for you.

In this article, I have attached a video tutorial to show how a double Lorentz function is fitted to a signal data to get the half width, peak center etc of the signal.

You can find the video in the link here.

Normalization between -1 and 1


Normalization between -1 and 1

Normalisation (or Normalization) is a very elegant and a necessary statistical process of data analysis. It can have a wide variety of meanings depending on the data and the output we wish for. Some of the common meanings of data normalization are as below:

There are various equations and methods to do the statistical normalization.  Readers can always follow a text book to read more about the statistical parts and the maths of the normalization.

In this post, I talk about an algorithm to normalize a data between -1 and 1. Normalizing data between this range is a necessary part of the experimental analysis of data.

You can download the code from here.

The logic lies in the fact that , the program finds the max and min of the data set and averages the rest of the data set from the difference of the max and min. By employing this, data redundancy and noise interference can also be reduced without doing further data averaging or processing.

The code can also be used to normalize the data between any user given range not necessarily -1 and 1.


Frequency of a variable in a cyclic data.

Frequency of a variable in a cyclic data.

In this post, a simple yet very necessary tool and requirement for data analysis has been discussed.  Suppose we have a large chunk of data which is cyclic or to say in simple words, we are looping a certain set of measurements as shown in the table.

Cyclic data

Cyclic data

The x values for all the cycles are the same from -90 to +90, but different Y1,Y2,Y3 and Y4 values. The table can be bigger depending on the number of loops we had set for our data acquisition program.

What we need as a final analysed data is a single X value column and single Y1, single Y2, single Y3, and a single Y4 values column; where each column contains the average value of each Y values over the cycles for a given x values.

This is quite a necessary and inevitable job when you have got a large set of noisy data, and you need to average the noise without losing any necessary information. So doing this in other programs like MS excel or other graph tools becomes tedious and also might be erroneous.

Here I mention about a MATLAB supplementary code, which makes this long, boring crucial work for us in no matter of time and also quite easily. The code is versatile to give the average, counts and other necessary information about the given data set.

The code can be downloaded here.


Further information about the code can be found here.


Image processing of multiple images simultaneously

Image processing of multiple images simultaneously

Image processing is one of the very important aspect of every branch of science. Scientists and students (including PhD’s), collect so much data, be that in ASCII format, or sometimes images. The information sometimes is buried deep down in the cornered pixels of an image. To decipher that, they need some tool to dig out this data from the buried levels of noise and blur. processor

One such tool is the image processing toolbox in Matlab. The toolbox has many attributes and functionalities which can be used straightforward to do the basic operations like contrast, brightness, colour etc.

From the science point of view, we people may need to find the mean, median, or may be the threshold of any given gray scale image.  In this post, I have attached a Matlab code which loads multiple 32 bit gray scale images stored in the same folder. The program then finds the median of each image in a given region of interest (ROI) and plots them for different image images.

The code is quite useful when you have  multiple images and you need to load them all together and do the operation. To mention here is that the image stored must have the same path, but file names in ascending (or descending) order .

Find the Matlab code here.