Fourier Transform for xlsx files from excel

8 Ansichten (letzte 30 Tage)
Iain Paul
Iain Paul am 17 Mai 2019
Kommentiert: Iain Paul am 17 Mai 2019
I have files from Excel in xlsx format that I want to find the fourier transform of. I have the code to generate a Fourier Transform but I'm not sure how to incorporate the files into the code itself.
clc;
clear all;+5
close all;
%This script generates a 20 Hz size wave of unit amplitude, of duration 1
%second, and time resolution of 1E-2 seconds; then calculates the
%single-sided Fourier transform and plots both quantities
dt=1E-2; %s
T=1; %s
f_sig=20; %Hz
t=[0:dt:T-dt]; %time vector
y=sin(2*pi*f_sig*t); %amplitude vector
subplot(121)
plot(t,y,'-b','LineWidth',2); %plot the time series
xlabel('time (s)');
ylabel('Amplitude (V)');
title('Time domain signal')
set(gca,'FontSize',20,'LineWidth',2)
F = fft(y); %calculate the Fast Fourier Transform
Fs=1/dt; %define the sampling frequency
L=length(t); %define the length of your time and amplitude vectors
P2 = abs(F/L); %take the normalized amplitude of your Fourier transform
P1 = P2(1:L/2+1); %adjust the length and scale to get single sided spectrum
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L; %create your frequency vector
subplot(122)
plot(f,P1,'-r','LineWidth',2); %plot the Fourier transform
xlabel('frequency (Hz)');
ylabel('Amplitude (V)');
set(gca,'FontSize',20,'LineWidth',2)
title('Fourier Transform')
xlim([0 50])
  6 Kommentare
Guillaume
Guillaume am 17 Mai 2019
if you're using code you don't understand, it's no surprise you get problem.
In the code you've shown num is meant to be the number of input files. The code is meant to extract column B2:B15039 of each file and sum them all. Except it's full of bugs.
sprintf was the correct function to use. The comment is simply wrong. The line is meant to construct a filename, except that the format was forgotten, so it always construct the name of the first file.
You should certainly not use fopen on an excel file (unless you know how to decode an excel file at the binary level).
Iain Paul
Iain Paul am 17 Mai 2019
Yeah sorry, my professors gave us this code to use and it's been a headache to figure out. Thanks for the help though.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 17 Mai 2019
Read the content of the file with readmatrix (R2019a only), readtable, or xlsread and pass whatever it is you want from that to fft or fft2.
Without more details about what's in the file, that's all that can be said.
  2 Kommentare
Iain Paul
Iain Paul am 17 Mai 2019
The excel files is just time in the x axis (column one) and voltage in the y axis (column 2).
Would the code be similar to this?
[X,TXT,RAW] = xlsread('yourfile.xls');
xdft = fft(X(:,2));
Guillaume
Guillaume am 17 Mai 2019
Using xlsread it would probably be:
data = xlsread('yourfile.xls'); %no point in asking for text and raw if you don't use them
xdft = fft(data(:, 2));
Personally, I'd use readtable to make it easier to understand what is what:
data = readtable('yourfile.xls', 'ReadVariableNames', false);
data.Properties.VariableNames = {'time', 'voltage'};
xdft = fft(data.Voltage);

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by