You cannot get a representation that is accurate both in the time and frequency domains. This is the uncertainty principle of the Fourier transform.
What you can do is trade off time and frequency resolution by changing the window length. Compare the two spectrograms below, obtained with different window lengths. The signal (figure 1) is similar to that in your question.
- The first spectrogram (figure 2) uses a long window, which gives good frequency resolution but poor time resolution. Note how the signal frequency of 10 Hz is resolved, but time information is very coarse.
- Conversely, the second spectrogram (figure 3) uses a short window, which provides good time resolution but poor frequency resolution. As is seen, the signal frequency cannot be resolved, but its time location and shape are more accurate.
% Define signal
fs = 500; % sampling frequency
t = 0:1/fs:6; % time axis
fm = 10; % signal (carrier) frequency
s = cos(2*pi*fm*t).* exp(-5*(t-2).^2);
figure
plot(t,s)
% Spectrogram with long window
figure
nfft = 500;
window = hamming(nfft);
spectrogram(s,window,[],nfft,fs), view([90 -90])
% Spectrogram with short window
figure
nfft = 50;
window = hamming(nfft);
spectrogram(s,window,[],nfft,fs), view([90 -90])


