Plotting an amplitude spectrum of a .txt file

11 views (last 30 days)
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!

Accepted Answer

Star Strider
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))
Maximum Amplitude = 0.0935 Frequency = 2.2417 Hz
.
EDIT — (9 Jun 2021 at 16:38)
Corrected typographical error:
As.XScale = 'log';
to:
Ax.XScale = 'log';
.

More Answers (1)

Scott MacKenzie
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);

Community Treasure Hunt

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

Start Hunting!