i would like to find power spectral density from pressure data.

7 次查看(过去 30 天)
Sampling_frequency = 400000;
Nfft = 8192; % FFT size
Overlap = 0.8; % Overlap percentage
No_of_segments = 2132; % Number of segments
window = hann(nfft); % Hanning window for PSD calculation
pressure_data = load(pressure_data.mat); % Load data if necessary
Mean_pressure = mean(pressure_data);
Pressure_data_detrended = pressure_data mean_pressure;
Segment_length= floor(length(pressure_data_detrended) / no_of_segments);
Overlap_samples = floor(segment_length * overlap);
Y = fft(pressure_data_detrended, nfft);
F = (0:nfft-1) * (sampling_frequency / nfft);
Figure;
Plot(f, abs(Y));
Title(FFT of Unsteady Pressure Data);
Xlabel(Frequency (Hz));
Ylabel(Magnitude);
Grid on;
[pxx, f_pxx] = pwelch(pressure_data_detrended, window, overlap_samples, nfft, sampling_frequency);
Figure;
Semilogy(f_pxx, pxx);
Title(Power Spectral Density of Unsteady Pressure Data);
Xlabel(Frequency (Hz));
Ylabel(Power/Frequency (dB/Hz));
Grid on;
  3 个评论
Sam Chak
Sam Chak 2025-2-19
From the code, it is likely that you copied the code from a typesetting format that automatically converts the starting letter of the first word or letter in every line into an uppercase. Would you fix them and replace the curly apostrophe with straight apostrophe?
Please also provide the data "pressure_data.mat". Click this icon to attach the data file.
dpb
dpb 2025-2-19
@solleti prabhakarchary, @Sam Chak read the code more carefully than I; I just selected and applied the "Code" formatting.
MATLAB is case-sensitive so all the MATLAB functions like figure, plot, title, x/ylabel, ... are going to fail because of the uppercase initial letters in your code...fix all that as well as the apostrophes around the character strings and then it will at least run.
Use the builtin MATLAB editor to prepare code; it doesn't do stuff like that if it was a tool/application that caused it or if you did type it and upper-cased out of inadvertent habit or the like, "don't do that!!!" <vbg>

请先登录,再进行评论。

回答(1 个)

Umar
Umar 2025-2-20
编辑:Umar 2025-2-20

Hi @solleti prabhakarchary ,

After reviewing your comments and achieving the desired outcome, not having access to pressure.mat file, I had to create synthetic pressure data, apply the necessary signal processing techniques, and visualize the results. Below is a detailed MATLAB code that fulfills your requirements.

% Parameters
sampling_frequency = 400000; % Sampling frequency in Hz
Nfft = 8192; % FFT size
overlap = 0.8; % Overlap percentage
no_of_segments = 2132; % Number of segments
window = hann(Nfft); % Hanning window for PSD calculation
% Generate synthetic pressure data
t = 0:1/sampling_frequency:1; % Time vector for 1 second
pressure_data = 0.5 * sin(2 * pi * 1000 * t) + randn(size(t)) * 0.1; 
 % Synthetic     pressure data with noise
% Detrending the pressure data
mean_pressure = mean(pressure_data);
pressure_data_detrended = pressure_data - mean_pressure;
% Segment length and overlap samples
segment_length = floor(length(pressure_data_detrended) / no_of_segments);
overlap_samples = floor(segment_length * overlap);
% FFT of the detrended pressure data
Y = fft(pressure_data_detrended, Nfft);
f = (0:Nfft-1) * (sampling_frequency / Nfft); % Frequency vector
% Plotting the FFT
figure;
plot(f, abs(Y));
title('FFT of Unsteady Pressure Data');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
xlim([0 sampling_frequency/2]); 
 % Limit x-axis to Nyquist frequency axis tight;
% Power Spectral Density calculation using Welch's method
[pxx, f_pxx] = pwelch(pressure_data_detrended, window, overlap_samples, 
 Nfft, sampling_frequency);
% Plotting the Power Spectral Density
figure;
semilogy(f_pxx, pxx);
title('Power Spectral Density of Unsteady Pressure Data');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;
xlim([0 sampling_frequency/2]); % Limit x-axis to Nyquist frequency
axis tight;

Please see attached.

1. Parameters Initialization: The code begins by defining the necessary parameters such as sampling frequency, FFT size, overlap percentage, and the number of segments. 2. Synthetic Data Generation: A synthetic pressure signal is created using a sine wave combined with random noise. This simulates real-world pressure data. 3. Detrending: The mean of the pressure data is calculated and subtracted from the original data to remove any DC offset, ensuring that the analysis focuses on the variations in the signal. 4. Segment Length and Overlap Calculation: The segment length is determined based on the total length of the detrended data and the specified number of segments. The number of overlapping samples is calculated accordingly. 5. FFT Calculation: The Fast Fourier Transform (FFT) is computed on the detrended pressure data, and a frequency vector is generated for plotting. 6. FFT Plotting: The magnitude of the FFT is plotted against frequency, providing insight into the frequency components of the pressure data. 7. Power Spectral Density Calculation: The pwelch function is used to compute the Power Spectral Density using Welch's method, which averages the periodograms of overlapping segments of the signal. 8. PSD Plotting: Finally, the PSD is plotted on a logarithmic scale, allowing for a clear visualization of the power distribution across frequencies.

By following the outlined steps, you can adapt the code to analyze real pressure data as needed. If you have any further questions or require additional modifications, please feel free to ask.

类别

Help CenterFile Exchange 中查找有关 Parametric Spectral Estimation 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by