How to extract data from txt file and plot spectogram?

19 ビュー (過去 30 日間)
Rahul 2023 年 6 月 3 日
コメント済み: Star Strider 2023 年 6 月 15 日
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);
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);
plot(f, fft_tt1);


Star Strider
Star Strider 2023 年 6 月 3 日
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;
pspectrum(y_data, Fs, 'spectrogram') % Plot 'pspectrum' 'spectrogram'
[sp,fp,tp] = pspectrum(y_data,fs,"spectrogram"); % Return Values, Then Plot
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.
  37 件のコメント
Star Strider
Star Strider 2023 年 6 月 15 日
Are you certain this is correct:
myfilename = sprintf('OBP%dN.txt', k);
MATLAB is case-sensitive, and the file names must match exactly, even with respect to case, and the other file names were all lower-case. If that is the problem, the code should throw an error about not being able to find the file. Are there any errors?
So the first thing to do is to check to see what ‘mydata’ contains. Be certain that it is not empty.


その他の回答 (1 件)

Diwakar Diwakar
Diwakar Diwakar 2023 年 6 月 3 日
Try this code. May be this code will help you.
% Specify the path to your text file
file_path = 'path/to/your/file.txt';
% Read the data from the text file
data = dlmread(file_path);
% Extract the time and amplitude columns
time = data(:, 1);
amplitude = data(:, 2);
% Set the parameters for the spectrogram
window_size = 256; % Size of the window for the spectrogram
overlap = 128; % Overlap between consecutive windows
% Compute the spectrogram
[~, frequency, time_spectrogram, power] = spectrogram(amplitude, window_size, overlap, [], 'onesided', 'yaxis');
% Plot the spectrogram
imagesc(time_spectrogram, frequency, 10*log10(power));
axis xy; % Flip the y-axis direction
colormap jet; % Choose a colormap
colorbar; % Add a colorbar for reference
  4 件のコメント
Star Strider
Star Strider 2023 年 6 月 4 日
@Rahul — We already covered this and I told you how to fix it.
If we are going to solve it, you need to attach your data.


Community Treasure Hunt

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

Start Hunting!

Translated by