Where is the data?
4 views (last 30 days)
Show older comments
howdy again,
I am plotting the fourier amplitude spectrum for the mauna loa data yet when I run the code, the plot is empty. is it an axes problem? and if so, how do I fix it so that the x axis is still in 10^nth hertz.
% Load the data
filename = 'maunaloa_weekly.csv';
data = readtable(filename, 'ReadVariableNames', false, 'Delimiter', ',');
variableNames = {'Date', 'Values'};
% Assign variable names to the table
data.Properties.VariableNames(1:2) = variableNames;
% Convert the dates to datetime format
dates = datetime(data.Date, 'Format', 'dd/MM/yyyy');
Values = data.Values;
% Create a time vector using datetime
time_vector = dates;
% Plot the data
figure;
subplot(2, 1, 1);
plot(dates, Values, 'LineWidth', 2);
title('Maunaloa Weekly Data');
xlabel('Time');
ylabel('Floating Point Values');
% Fourier analysis plot
subplot(2, 1, 2);
% Time interval between data points
dt = days(7); % Weekly data
% Perform FFT
N = length(Values);
Fs = 1/days(dt); % Sampling frequency
frequencies = Fs*(0:(N/2))/N;
fft_values = fft(Values);
amplitudes = 2/N * abs(fft_values(1:N/2+1));
% Plot the amplitude spectrum with log scale on x-axis
semilogx(frequencies, amplitudes, 'LineWidth', 2);
title('Fourier Amplitude Spectrum');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
2 Comments
the cyclist
on 1 Feb 2024
Can you upload the maunaloa_weekly.csv file? You can use the paper clip icon in the INSERT section of the toolbar.
Accepted Answer
Star Strider
on 1 Feb 2024
% Load the data
filename = 'maunaloa_weekly.csv';
data = readtable(filename, 'ReadVariableNames', false, 'Delimiter', ',');
variableNames = {'Date', 'Values'};
% Assign variable names to the table
data.Properties.VariableNames(1:2) = variableNames;
% Convert the dates to datetime format
dates = datetime(data.Date, 'Format', 'dd/MM/yyyy');
Values = data.Values;
% Create a time vector using datetime
time_vector = dates;
% Plot the data
figure;
subplot(2, 1, 1);
plot(dates, Values, 'LineWidth', 2);
title('Maunaloa Weekly Data');
xlabel('Time');
ylabel('Floating Point Values');
% Fourier analysis plot
subplot(2, 1, 2);
% Time interval between data points
dt = days(7); % Weekly data
% Perform FFT
fprintf('There are %d NaN elements in ''Values''.\n',nnz(isnan(Values)))
Values = fillmissing(Values,'linear');
N = length(Values);
Fs = 1/days(dt); % Sampling frequency
frequencies = Fs*(0:(N/2))/N;
fft_values = fft(Values);
amplitudes = 2/N * abs(fft_values(1:N/2+1));
% Plot the amplitude spectrum with log scale on x-axis
semilogx(frequencies, amplitudes, 'LineWidth', 2);
title('Fourier Amplitude Spectrum');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
Make appropriate changes to get the result you want.
.
0 Comments
More Answers (0)
See Also
Categories
Find more on Annotations 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!