0% found this document useful (0 votes)
19 views7 pages

'1.xls' '2.xls' '3.xls' '4.xls' '5.xls' '6.xls'

The document outlines a MATLAB script that processes six trials of acceleration data from Excel files. It includes steps for filtering the data, performing a Fast Fourier Transform (FFT), and saving the results to .mat and Excel files. Additionally, it generates plots for the original and filtered signals as well as a superimposed frequency graph for all trials.

Uploaded by

Hiro Sugasawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

'1.xls' '2.xls' '3.xls' '4.xls' '5.xls' '6.xls'

The document outlines a MATLAB script that processes six trials of acceleration data from Excel files. It includes steps for filtering the data, performing a Fast Fourier Transform (FFT), and saving the results to .mat and Excel files. Additionally, it generates plots for the original and filtered signals as well as a superimposed frequency graph for all trials.

Uploaded by

Hiro Sugasawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

% Define filenames for the 6 trials

filenames = {'1.xls', '2.xls', '3.xls', '4.xls', '5.xls', '6.xls'};


numFiles = length(filenames);

% Sampling frequency
fs = 250; % Adjust based on your data

% Parameters for filtering


sampleRate = 2000;
cutoffFreq = 100;
[b, a] = butter(2, cutoffFreq / (sampleRate / 2), 'low');

% Loop through each file to process, filter, perform FFT, and save
for trial = 1:numFiles
filename = filenames{trial};

% Set up options for reading the data


opts = detectImportOptions(filename);
opts.VariableNamingRule = 'preserve';
dataTable = readtable(filename, opts);

% Display column names for verification


disp(['File ', num2str(trial), ' column names:']);
disp(dataTable.Properties.VariableNames);

% Extract signal and time columns


signal = dataTable.("Acceleration z (m/s^2)") / 9.81; % Normalize by g
time = dataTable.("Time (s)"); % Time column

% Ensure signal data is numeric


if ~isnumeric(signal)
error('The signal data is not numeric.');
end

% Remove DC offset
unfilteredAcceleration = signal - mean(signal);

% Apply low-pass filter


filteredAcceleration = filtfilt(b, a, unfilteredAcceleration);

% Perform FFT on filtered signal


n = length(filteredAcceleration);
f = (0:(n-1)) * (fs / n);
y = fft(filteredAcceleration);
magnitude = abs(y) / n; % Normalize magnitude by number of points

% Set the starting magnitude to 0


magnitude(1) = 0;

% Save FFT data to a .mat file


save(['TRIAL', num2str(trial), '_STRONG_AXIS.mat'], 'f', 'magnitude');

1
% Export filtered data and FFT results to Excel
outputFileName = ['Filtered_Data_' num2str(trial) '.xlsx'];
filteredDataTable = table(time, filteredAcceleration, 'VariableNames',
{'Time', 'Filtered_Acceleration'});
fftResultsTable = table(f(:), magnitude(:), 'VariableNames',
{'Frequency_Hz', 'Amplitude'});
writetable(filteredDataTable, outputFileName, 'Sheet', 'Filtered Data');
writetable(fftResultsTable, outputFileName, 'Sheet', 'FFT Results');

% Plot original and filtered signal


figure;
subplot(3, 1, 1);
plot(time, unfilteredAcceleration, 'k');
title(['Unfiltered Signal - Trial ', num2str(trial)]);
xlabel('Time (s)');
ylabel('Acceleration (g)');

subplot(3, 1, 2);
plot(time, filteredAcceleration, 'b');
title(['Filtered Signal - Trial ', num2str(trial)]);
xlabel('Time (s)');
ylabel('Acceleration (g)');

% Plot FFT Magnitude


half_n = floor(n / 2);
subplot(3, 1, 3);
plot(f(1:half_n), magnitude(1:half_n), 'r');
title(['FFT Magnitude - Trial ', num2str(trial)]);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
end

% Superimpose frequency graphs from saved .mat files


figure;
colors = ['r', 'g', 'b', 'c', 'm', 'y']; % Define colors for each trial

for trial = 1:numFiles


% Load FFT data
data = load(['TRIAL', num2str(trial), '_STRONG_AXIS.mat']);
x = data.f;
y = data.magnitude;

% Plot each trial's FFT magnitude with different colors


plot(x(1:half_n), y(1:half_n), 'Color', colors(trial), 'DisplayName',
['TRIAL', num2str(trial)]);
hold on;
end

% Label the superimposed plot


xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Superimposed Frequency Graph of Six Trials');
legend('show');

2
grid on;
hold off;

File 1 column names:


Columns 1 through 3

{'Time (s)'} {'Acceleration x...'} {'Acceleration y...'}

Columns 4 through 5

{'Acceleration z...'} {'Absolute accel...'}

File 2 column names:


Columns 1 through 3

{'Time (s)'} {'Acceleration x...'} {'Acceleration y...'}

Columns 4 through 5

{'Acceleration z...'} {'Absolute accel...'}

File 3 column names:


Columns 1 through 3

{'Time (s)'} {'Acceleration x...'} {'Acceleration y...'}

Columns 4 through 5

{'Acceleration z...'} {'Absolute accel...'}

File 4 column names:


Columns 1 through 3

{'Time (s)'} {'Acceleration x...'} {'Acceleration y...'}

Columns 4 through 5

{'Acceleration z...'} {'Absolute accel...'}

File 5 column names:


Columns 1 through 3

{'Time (s)'} {'Acceleration x...'} {'Acceleration y...'}

Columns 4 through 5

{'Acceleration z...'} {'Absolute accel...'}

File 6 column names:


Columns 1 through 3

{'Time (s)'} {'Acceleration x...'} {'Acceleration y...'}

Columns 4 through 5

3
{'Acceleration z...'} {'Absolute accel...'}

4
5
6
Published with MATLAB® R2024b

You might also like