0
votes

I would like to generate the spectrogram (power spectral density) from a temporal signal, having the frequency in Hz on the y-axis and time in seconds on the x-axis.

I have a sinusoidal signal generated like this:

dt = 0.04; % Integration time step in ms
T = 10000;  % simulation time in ms -> so 10 seconds of simulation
nt = round(T/dt); % simulation steps

x = sin(2*pi*frequency*(1:1:nt)*dt/1000 + phase);
figure; plot((1:1:nt)*dt/1000, x)

enter image description here

I plot the spectrogram / power spectral density as (p.s. I am not familiar with it):

fs = 1000/dt;
figure; spectrogram(x, [], [], [], fs, 'yaxis', 'psd')

enter image description here

I expected the plot to be Hz-vs-Seconds but I got kHz-vs-Seconds. Also by setting fs=1/dt; the plot becomes Hz-vs-Hours.

1
If you wanted Hz, can you just relabel the yTicks to be multipled by 1000? Then you have Hz and can change ylim to be whatever you need. Please tell me if I'm missing something.SecretAgentMan
Thank you @SecretAgentMan, by using ylim([0, 0.012]), yticks(yticks*1000), ylabel('Frequency (Hz)') seems to work. But I am struggling to make the labels of the yticks visible. Do you know how to solve it?Karl Alexius

1 Answers

1
votes

Execute your code like you do above but then instead of yticks(yticks*1000) do the following:

% Get yRuler-object
ax = gca;
yRuler = ax.YAxis;

% Loop through TickValues, multiply and insert into TickLabels cell-array
for n = 1:numel(yRuler.TickValues)
    yRuler.TickLabels{n} = num2str(yRuler.TickValues(n) * 1000);
end

Not particulary concise, but it should do the job.