Time-frequency analysis using spectogram

조회 수: 18 (최근 30일)
Shanmuka
Shanmuka 2023년 2월 17일
답변: Sulaymon Eshkabilov 2023년 2월 17일
% Load the measurement signal from a .mat file
x=load('signal27.mat');
% Set the parameters for the spectrogram
window_size = 1024; % Size of the analysis window
overlap = window_size/2; % Amount of overlap between windows
nfft = window_size; % Number of FFT points
fs = 1000; % Sampling rate
% Compute the spectrogram
[S, F, T] = spectrogram(x, window_size, overlap, nfft, fs);
% Plot the spectrogram
imagesc(T, F, abs(S));
axis xy;
xlabel('Time (s)');
ylabel('Frequency (Hz)');
WHen trying to run this code, it gives me error on 'pwelch'
Error using pwelch
Expected x to be one of these types:
single, double
Instead its type was struct.
Error in signal.internal.spectral.welchparse>parse_inputs (line 111)
validateattributes(x2,{'single','double'}, {'finite','nonnan'},'pwelch','x')
Error in signal.internal.spectral.welchparse (line 31)
parse_inputs(x1,esttype,varargin{:});
Error in pspectrogram (line 30)
[xw,nx,~,yw,ny,win,~,~,noverlap,~,~,options] = signal.internal.spectral.welchparse(x,esttype,inpArgs{:});
Error in spectrogram (line 191)
[varargout{1:nargout}] = pspectrogram({x},'spect',inpArgs{:});
Error in a (line 11)
[S, F, T] = spectrogram(x, window_size, overlap, nfft, fs);
Can anyone suggest what am i doing wrong here?
And also I need to use "spectogram" command to plot images.

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 17일
There are a couple of inconsistencies (fs value was incorrect and not read from the signal27.mat) in the code. Here is the corrected code:
% Load the measurement signal from a .mat file
x=load('signal27.mat').y;
fs = load('signal27.mat').Fs; % Sampling rate
% Set the parameters for the spectrogram
window_size = 1024; % Size of the analysis window
overlap = window_size/2; % Amount of overlap between windows
nfft = window_size; % Number of FFT points
% Compute the spectrogram
[S, F, T] = spectrogram(x, window_size, overlap, nfft,fs);
% Plot the spectrogram
imagesc(T, F, abs(S));
axis xy;
xlabel('Time (s)');
ylabel('Frequency (Hz)');
figure
spectrogram(x, 'yaxis')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by