Main Content

Resample and Filter a Nonuniformly Sampled Signal

A person recorded their weight in pounds during the leap year 2012. The person did not record their weight every day, so the data are nonuniform. Use the Signal Analyzer app to preprocess and study the recorded weight. The app enables you to fill in the missing data points by interpolating the signal to a uniform grid. (This procedure gives the best results if the signal has only small gaps.)

Load the data and convert the measurements to kilograms. The data file has the missing readings set to NaN. There are 27 data points missing, most of them during a two-week stretch in August.

wt = datetime(2012,1,1:366)';

load weight2012.dat
wgt = weight2012(:,2)/2.20462;

validpoints = ~isnan(wgt);
missing = wt(~validpoints);
ans = 12x1 datetime

Store the data in a MATLAB® timetable. Remove the missing points. Remove the DC value to concentrate on fluctuations. Convert the time information to a duration array by subtracting the first time point. For more details, see Data Types Supported by Signal Analyzer.

wgt = wgt(validpoints);
wgt = wgt - mean(wgt);

wt = wt(validpoints);
wt = wt - wt(1);

wg = timetable(wt,wgt);

Open Signal Analyzer and drag the timetable to a display. On the Display tab, click Spectrum to open a spectrum view. On the Time tab, select Show Markers. Zoom into the missing stretch by setting the Time Limits to 200 and 250 days.

Right-click the signal in the Signal table and select Duplicate. Rename the copy as Preprocessed by double-clicking the Name column in the Signal table. Leave the Preprocessed signal selected. On the Analyzer tab, click Preprocessing ▼ and select Resample. On the Resample tab that appears, enter a sample rate of 1 cycles/day and select the Shape Preserving Cubic method. Click Resample. Overlay the resampled signal on the display by selecting the check box next to its name.

Zoom out to reveal the data for the whole year. On the Spectrum tab, set the leakage to the maximum value. The spectra of the original and resampled signals agree well for most frequencies. The spectrum shows two noticeable peaks, one at around 0.14 cycles/day and the other at very low frequencies. To locate the peaks better, click Data Cursors ▼ and select Two. Place the cursors on the peaks. Hover over the frequency field of each cursor to get a more precise value of its location.

  • The medium-frequency peak is at 0.142857 = 1/7 cycles/day, which corresponds to a one-week cycle.

  • The low-frequency peak is at 0.004762 cycles/day, which corresponds to a 210-day cycle.

Remove the cursors by clicking the Data Cursors icon. Remove the original signal from the display. Filter the Preprocessed signal to remove the effects of the cycles.

  1. To remove the low-frequency cycle, highpass-filter the signal. On the Analyzer tab, select Highpass. On the Highpass tab that appears, enter a passband frequency of 0.05 cycles/day. Use the default values of the other parameters. Click Highpass.

  2. To remove the weekly cycle, bandstop-filter the signal. On the Analyzer tab, click Preprocessing ▼ and select Bandstop. On the Bandstop tab that replaces the Highpass tab, enter a lower passband frequency of 0.135 cycles/day and a higher passband frequency of 0.15 cycles/day. Use the default values of the other parameters. Click Bandstop.

The preprocessed signal shows less fluctuation than the original. The shape of the signal suggests the person's weight varies less in the summer months than in winter, but that may be an artifact of the resampling. Click the icon on the Info column in the Signal table entry for the Preprocessed signal to see the preprocessing steps performed on it.

To see a full summary of the preprocessing steps, including all the settings you chose, click Generate Function on the Analyzer tab. The generated function appears in the MATLAB® Editor.

function [y,ty] = preprocess(x,tx)
%  Preprocess input x
%    This function expects an input vector x and a vector of time values
%    tx. tx is a numeric vector in units of seconds.
%    Follow the timetable documentation (type 'doc timetable' in
%    command line) to learn how to index into a table variable and its time
%    values so that you can pass them into this function.

% Generated by MATLAB(R) 9.5 and Signal Processing Toolbox 8.1.
% Generated on: 08-Jun-2018 14:35:38

targetSampleRate = 1.1574074074074073e-05;
[y,ty] = resample(x,tx,targetSampleRate,'pchip');
Fs = 1/mean(diff(ty)); % Average sample rate
y = highpass(y,5.787e-07,Fs,'Steepness',0.85,'StopbandAttenuation',60);
y = bandstop(y,[1.5625e-06 1.73611111111111e-06],Fs,'Steepness',0.85,'StopbandAttenuation',60);

See Also



Related Examples

More About