How to extract data from txt file and plot spectogram?

Illustration
Rahul - 2023-06-08T12:08:21+00:00
Question: How to extract data from txt file and plot spectogram?

Hello, I have a large sample of data (100000 samples) in txt file consisting of time and amplitude of the signal.   I want to plot spectogram. The code is not at all able to extract the .txt file at all along the path.   Kindly advice me as how to extract data from txt file when data is very large as it is in my case and hence to plot the spectogram.   The code is as below:     close all; freq_sam = 100000; % samples in tt1 data period_tt1 = 0.005; % time period freq_tt1 = 1/period_tt1; % frequency data = importdata('D:\Data_tokamak\data\obp.txt'); x_data = data(:,1); y_data = data(:,2); disp(x_data); disp(y_data); signal_tt1 = data; fft_tt1 = fft(signal_tt1); % fourier transform fft_tt1 = fftshift(fft_tt1); % performing fft shift f = freq_sam/2*linspace(-1,1,freq_sam); figure; plot(f, fft_tt1);  

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

I prefer the pspectrum function for these analyses. See specifically Spectrogram and Reassigned Spectrogram of Chirp for an example of returning the data and plotting it as well.

 

data = readmatrix('D:\Data_tokamak\data\obp.txt');
x_data = data(:,1);
y_data = data(:,2);

Ts = mean(diff(x_data))
Fs = 1/Ts;

figure
pspectrum(y_data, Fs, 'spectrogram')                            % Plot 'pspectrum' 'spectrogram'

[sp,fp,tp] = pspectrum(y_data,fs,"spectrogram");                % Return Values, Then Plot

figure
waterfall(fp,tp,sp')
set(gca,XDir="reverse",View=[60 60])
ylabel("Time (s)")
xlabel("Frequency (Hz)")
Be certain that the ‘x_data’ are sampled uniformly. The easiest way to do that is to calculate the standard deviation (std) of ‘diff(x_data)’ and compare it to ‘Ts’. It should be vanishingly small, and preferably less than 1E-6 of the ‘Ts’ value.
 
It would help to have ‘obp.txt’ to work with.
 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!