Plotting an amplitude spectrum of a .txt file
11 views (last 30 days)
Show older comments
I wanted to ask if someone can help me plot an amplitude spectrum (log-log-scale) of a .txt file. In the file is a time series that has a seismic signal recorded at time intervals of 0.005s.
I would be very grateful!
0 Comments
Accepted Answer
Star Strider
on 9 Jun 2021
Edited: Star Strider
on 9 Jun 2021
Try this —
D1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/647750/spec.txt');
signal = D1;
L = numel(signal); % Signal Length
Ts = 0.005; % Sampling Interval (sed)
t = linspace(0, L-1, L); % Time Vector
figure
plot(t, signal)
grid
title('Time Domain')
xlabel('Time (s)')
ylabel('Amplitude (Units)')
Fs = 1/Ts; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
NFFT = 2^nextpow2(L); % FFT Length
FTsignal = fft(signal,NFFT)/L; % Normalised Fourier Transform
Fv = linspace(0,1,fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FTsignal(Iv))*2)
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude (Umits)')
Ax = gca;
Ax.XScale = 'log';
Ax.YScale = 'log';
[maxAmp,idx] = max(abs(FTsignal(Iv))*2);
fprintf('Maximum Amplitude = \t%.4f\nFrequency = \t\t%.4f Hz',maxAmp,Fv(idx))
.
EDIT — (9 Jun 2021 at 16:38)
Corrected typographical error:
As.XScale = 'log';
to:
Ax.XScale = 'log';
.
0 Comments
More Answers (1)
Scott MacKenzie
on 9 Jun 2021
This seems to work...
% load seismic signal data
ss = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/647750/spec.txt');
t = 0.005 * (1:length(ss)); % time
% plot using log-log scale
loglog(t,ss);
0 Comments
See Also
Categories
Find more on Seismology 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!