No BSD License  

Highlights from
changeSampleFreq

from changeSampleFreq by Cornelis P. Bogerd
Easily change the sample frequency of a data-set

changeSampleFreq(dataOld, freqOld, freqNew, qFilter, qPlot, qLastValue)
function dataNew = changeSampleFreq(dataOld, freqOld, freqNew, qFilter, qPlot, qLastValue)

%
% dataNew = changeSampleFreq(dataOld, freqOld, freqNew, qFilter, qPlot)
% 
% this function gives the following output:
%   *the original data-set but with the chosen sample frequency
% 
%
% this function uses the following input:
%   *dataOld: the original data-set (as a column, multiple columns are possible)
%   *freqOld: the sample frequency of the original data-set (Hz [1/s])
%   *FreqNew: the desired sample frequency (Hz [1/s])
%   *qFilter (optional): switch for application of data filtering (1 =
%   yes), default is no filtering.
%   *qPlot (optional): switch for creating a random plot of one of the data-sets
%   (columns) to serve as a visual check. default is no visualization.
%   *qLastValue: adds the last value (filtered if qFilter = true), default
%   is false.
%
%
% Created by C.P. (Niels) Bogerd at Empa
% bogerd@nielsbogerd.com
% First version (1.0): 2007.08.28
% Current version (1.1): 2008.01.14 (qLastValue added)
% 

%% checking input and setting defaults
% checking input
if nargin < 3
   error('At least three input arguments are required.')
elseif nargin == 3
    qFilter = false;
    qPlot = false;
    qLastValue = false;
elseif nargin == 4
    qPlot = false;
    qLastValue = false;
elseif nargin == 5
    qLastValue = false;
end

% set parameters
nReferences = round(freqOld/freqNew);
[row col] = size(dataOld);
tOld = [0 : 1 / freqOld : (row - 1) / freqOld]';
tNew = [0 : 1 / freqNew : (row - 1) / freqOld]';

%% process data
% filter data
if freqOld > freqNew && qFilter == true
    dFilt = filtfilt(repmat(1,[nReferences,1]), nReferences, dataOld);
else
    dFilt = dataOld;
end

% adapt the sample frequency
for i = 1 : col
    dataNew(:, i) = interp1(tOld, dFilt(:, i), tNew);
end

% add last value to data-set
if qLastValue == true
    dataNew = [dataNew; dFilt(end,:)];
    tNew = [tNew; tNew(end) + 1 / freqNew];
end

%% plot data for visual inspection
if qPlot == true
    % pick random sample
    if col > 1
        i = ceil(rand(1) * col);
    end

    % plot data
    plot(tOld, dataOld(:,i), '-r', tNew, dataNew(:,i), '-bo', 'MarkerEdgeColor','b',...
        'MarkerFaceColor','b', 'MarkerSize', 5);
    legend('Original data-set','Adapted data-set', 'Location', 'Best');
end

Contact us at files@mathworks.com