Error using fft on code

조회 수: 3 (최근 30일)
Matthew Gill
Matthew Gill 2024년 4월 22일
답변: Gayatri 2024년 4월 24일
% Not sure what I am doing wrong to be getting an error with the data type
% Load data from Signal.mat file
load('Spec.mat');
% Number of rows (process realizations)
num_rows = size(Spec, 1);
% Number of samples per row
num_samples = size(Spec, 2);
% Calculate the power spectral density (PSD) for each row
psd_rows = zeros(num_rows, num_samples);
for i = 1:num_rows
psd_rows(i, :) = (abs(fftshift(fft(Spec(i, :)))).^2) / num_samples; <------------- error here
end
% Average the PSDs of all rows
psd_average = mean(psd_rows, 1);
% Sampling frequency (assuming 40 kHz)
Fs = 40e3;
% Frequency resolution
df = Fs / num_samples;
% Frequency axis
f_axis = (-num_samples/2 : num_samples/2 - 1) * df;
% Plot individual and composite PSDs
figure;
hold on;
for i = 1:num_rows
plot(f_axis, fftshift(psd_rows(i, :)));
end
plot(f_axis, fftshift(psd_average), 'k', 'LineWidth', 2);
xlabel('Frequency (Hz)');
ylabel('Power/Frequency');
title('Power Spectral Density (PSD)');
legend('Individual PSDs', 'Composite PSD');
  댓글 수: 1
Voss
Voss 2024년 4월 22일
What is the error message?
It will probably also help to have the file Spec.mat. You can upload it using the paperclip button.

댓글을 달려면 로그인하십시오.

답변 (1개)

Gayatri
Gayatri 2024년 4월 24일
Hi Matthew,
When you load a MAT file using "load" function, assign it to a variable. Data inside the structure can be accessed as below :
% Load data from Spec.mat file
Data = load('Spec.mat');
% Number of rows (process realizations)
num_rows = size(Data.Spec, 1);
I have created the sample Spec MAT file for testing purpose and not getting any error on given line:
% Number of signals (rows)
num_signals = 5;
% Number of samples (columns)
num_samples = 1024;
% Generate random data for simplicity
Spec = randn(num_signals, num_samples);
% Save the matrix to a .mat file
save('Spec.mat', 'Spec');
% Load data from Spec.mat file
Data = load('Spec.mat');
% Number of rows (process realizations)
num_rows = size(Data.Spec, 1);
% Number of samples per row
num_samples = size(Data.Spec, 2); % Corrected from 'size(Spec, 2);' to 'size(Data.Spec, 2);'
% Calculate the power spectral density (PSD) for each row
psd_rows = zeros(num_rows, num_samples);
for i = 1:num_rows
psd_rows(i, :) = (abs(fftshift(fft(Data.Spec(i, :)))).^2) / num_samples;
end
% Average the PSDs of all rows
psd_average = mean(psd_rows, 1);
% Sampling frequency (assuming 40 kHz)
Fs = 40e3;
% Frequency resolution
df = Fs / num_samples;
% Frequency axis
f_axis = (-num_samples/2 : num_samples/2 - 1) * df;
% Plot individual and composite PSDs
figure;
hold on;
for i = 1:num_rows
plot(f_axis, fftshift(psd_rows(i, :)));
end
plot(f_axis, fftshift(psd_average), 'k', 'LineWidth', 2);
xlabel('Frequency (Hz)');
ylabel('Power/Frequency');
title('Power Spectral Density (PSD)');
legend('Individual PSDs', 'Composite PSD');
I hope it helps!

카테고리

Help CenterFile Exchange에서 Parametric Spectral Estimation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by