Time-frequency analysis using spectogram
12 views (last 30 days)
Show older comments
Shanmuka
on 17 Feb 2023
Answered: Sulaymon Eshkabilov
on 17 Feb 2023
% 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.
0 Comments
Accepted Answer
Sulaymon Eshkabilov
on 17 Feb 2023
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 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
