Fourier Transform for xlsx files from excel
Show older comments
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 Comments
Iain Paul
on 17 May 2019
If you get an error, give us the full text of the error message. What does the num input command mean?
Note that none of the sprintf call in your code make any sense. The first two lack any formatting parameter to make use of the i, the 3rd one is pointless. Also note that sprintf is not a function that can open a file (for reading or anything else).
Iain Paul
on 17 May 2019
Iain Paul
on 17 May 2019
Guillaume
on 17 May 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
on 17 May 2019
Answers (1)
Guillaume
on 17 May 2019
0 votes
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 Comments
Iain Paul
on 17 May 2019
Guillaume
on 17 May 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);
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!