% Downsampling a given 'X' hz file to a input 'Y'hz file
%This script will load the file in a structure and append _YHz
% at the end of current variables. It will save the new file with the old
% name and append _down_sampled to it
% NOTE --> It will only DOWNSAMPLE the file. Although it can do up
% sampling, but it's not recommended to up sample data as it will just
% interpolate the data in between two points.
% You will need matlab SIGNAL PROCESSING TOOLBOX for this script to work
% In case you do not have access to the toolbox, please contact the author
% remote desktop matlab151a and matlab 151b have Signal Processing Toolbox
%% prompt dialog
tic;
prompt = {'Enter current data frequency[hz]:','Enter desired data frequency[hz]:'};
dlgtitle = 'Downsample data'; % dialog title
dims = [1 40];
definput = {'10','1'};
answer = inputdlg(prompt,dlgtitle,dims,definput);
%% post processing of dialoge
current_freq = str2double(cell2mat((answer(1,1))));
desired_freq = str2double(cell2mat(answer(2,1)));
dwn_smpl = current_freq/desired_freq; % desired downsampling frequency
str_var = ['_' num2str(desired_freq) 'Hz']; % var name append
%% file info
[f_name, f_path]= uigetfile({'*.mat'}, 'Select a mat file'); % select the file(s) from gui
File = [f_path '\' f_name]; % getting the file path and name
S = load(File); % loading file to a structure
fn = fieldnames(S); % get the variable names in the file.
Fc = length(fn); % num of variables in the structure file
for k = 1:1:Fc; % running look to process data
F = fn{k}; % field variable name
F = strrep(F, 'YOUR TEXT GOES HERE', ''); % replacing undesireable text from variable name
if dwn_smpl > 1
S.([fn{k} str_var]) = downsample(S.(F),dwn_smpl); % downsampling data
else
S.([fn{k} str_var]) = upsample(S.(F),dwn_smpl); % up sampling data
end
end
%% saving file based off up sampling or down sampling
if dwn_smpl > 1
save([f_name(1:end-4) '_down_sampled.mat'], '-struct', 'S') % extracting data from structure and saving as .mat file
else
save([f_name(1:end-4) '_up_sampled.mat'], '-struct', 'S') % extracting data from structure and saving as .mat file
end
disp (['All variables down sampled to: ' num2str(desired_freq) 'Hz']);
toc