plotting power spectrum density
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have 2 .dat file with me one of amplitude and one of time and I want to do the powerspectrum analysis of the data. Can anyone please help me in creating a time vector for doing FFT
採用された回答
Hassaan
2024 年 5 月 21 日
% Load the data from the .dat files
amplitude = load('amplitude.dat');
time = load('time.dat');
% Check if time data is provided, otherwise create time vector
if isempty(time)
Fs = 1000; % Sampling frequency in Hz
Ts = 1 / Fs; % Sampling interval in seconds
time = (0:length(amplitude)-1) * Ts; % Time vector
end
% Compute FFT
L = length(amplitude); % Length of the signal
Y = fft(amplitude); % Compute the FFT
P2 = abs(Y / L); % Two-sided spectrum
P1 = P2(1:L/2+1); % Single-sided spectrum
P1(2:end-1) = 2 * P1(2:end-1);
f = Fs * (0:(L/2)) / L; % Frequency vector
% Calculate Power Spectrum Density (PSD)
PSD = (1 / (Fs * L)) * (abs(Y(1:L/2+1)).^2);
PSD(2:end-1) = 2 * PSD(2:end-1);
% Plot the PSD
figure;
plot(f, 10 * log10(PSD));
title('Power Spectrum Density');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;
Notes
- Sampling Frequency (Fs): Ensure that you know the correct sampling frequency of your data. If it is not 1000 Hz, replace Fs = 1000 with the correct value.
- Units: The PSD is plotted in decibels (dB/Hz). The conversion is done using 10 * log10(PSD).
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
5 件のコメント
Kashish
2024 年 5 月 21 日
I already have the time
Kashish
2024 年 5 月 21 日
I already have these files and I have used this code for extraction of data
fileinfo = dir('amp-A-016.dat');
num_samples = fileinfo.bytes/2;
fid = fopen('amp-A-016.dat', 'r');
v = fread(fid, num_samples, 'int16');
fclose(fid);
v = v * 0.0000374;
%%%Time Data input
fileinfo = dir('time.dat');
num_samples = fileinfo.bytes/4; % int32 = 4 bytes
fid = fopen('time.dat', 'r');
t = fread(fid, num_samples, 'int32');
fclose(fid);
% t = t / frequency_parameters.amplifier_sample_rate; % sample rate from header file
t=t/20000;
what would be the time vector if I already have the time data
please help me
Hassaan
2024 年 5 月 21 日
% Load the amplitude data
fileinfo = dir('amp-A-016.dat');
num_samples = fileinfo.bytes / 2;
fid = fopen('amp-A-016.dat', 'r');
amplitude = fread(fid, num_samples, 'int16');
fclose(fid);
amplitude = amplitude * 0.0000374;
% Load the time data
fileinfo = dir('time.dat');
num_samples = fileinfo.bytes / 4; % int32 = 4 bytes
fid = fopen('time.dat', 'r');
time = fread(fid, num_samples, 'int32');
fclose(fid);
time = time / 20000; % Adjust according to your sample rate
% Compute the sampling frequency (Fs) from time data
Fs = 1 / mean(diff(time)); % Sampling frequency in Hz
% Compute FFT
L = length(amplitude); % Length of the signal
Y = fft(amplitude); % Compute the FFT
P2 = abs(Y / L); % Two-sided spectrum
P1 = P2(1:L/2+1); % Single-sided spectrum
P1(2:end-1) = 2 * P1(2:end-1);
f = Fs * (0:(L/2)) / L; % Frequency vector
% Calculate Power Spectrum Density (PSD)
PSD = (1 / (Fs * L)) * (abs(Y(1:L/2+1)).^2);
PSD(2:end-1) = 2 * PSD(2:end-1);
% Plot the PSD
figure;
plot(f, 10 * log10(PSD));
title('Power Spectrum Density');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;
Kashish
2024 年 5 月 21 日
thank you the code is working what if I want to do multitaper powerspectrum density method
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Fourier Analysis and Filtering についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
