How to extract a part of the spectrogram?

8 ビュー (過去 30 日間)
Antonio Spera
Antonio Spera 2020 年 12 月 3 日
コメント済み: Antonio Spera 2020 年 12 月 3 日
Hi, I need help. How can I crop a part of the spectrogram? For example in the figure below I need to cut out the part of the spectrogram marked in red with the letter "A" and save it as an image.
Thank you for your help.

採用された回答

Adam Danz
Adam Danz 2020 年 12 月 3 日
編集済み: Adam Danz 2020 年 12 月 3 日
Three approaches to selecting a range within a spectogram
Use saveas to save figures as any format.
1. Simple xlim & ylim
A simple approach that will not result in the loss of data would be to set xlim and ylim.
Demo:
% Full spectrogram
N = 512;
n = 0:N-1;
x = exp(1j*pi*sin(8*n/N)*32);
figure()
spectrogram(x,32,16,64,'centered','yaxis')
title('Full Spectrogram')
rectX = [120 270]; % x limits of selected range
rectY = [-.8 .8]; % y limits of selected range
ax = gca();
axis(ax, 'tight')
rectangle(ax, 'Position', [rectX(1), rectY(1), range(rectX), range(rectY)])
% Limit the visible range
figure()
spectrogram(x,32,16,64,'centered','yaxis')
ax = gca();
rectX = [120 270]; % x limits of selected range
rectY = [-.8 .8]; % y limits of selected range
xlim(ax, rectX)
ylim(ax, rectY)
title('Limited range using xlim & ylim')
2. Maintain DataAspectRatio
figure()
spectrogram(x,32,16,64,'centered','yaxis')
ax = gca();
originalDAR = ax.DataAspectRatio;
rectX = [120 270]; % x limits of selected range
rectY = [-.8 .8]; % y limits of selected range
xlim(ax, rectX)
ylim(ax, rectY)
ax.DataAspectRatio = originalDAR;
title('Maintain data asepct ratio')
3. Maintain the region's area within the figure
figure()
spectrogram(x,32,16,64,'centered','yaxis')
ax = gca();
axis(ax, 'tight')
rectX = [120 270]; % x limits of selected range
rectY = [-.8 .8]; % y limits of selected range
% Scale axis position
axPos = ax.Position;
xl = xlim(ax);
yl = ylim(ax);
xScale = range(rectX)/range(xl);
yScale = range(rectY)/range(yl);
xShift = (rectX(1)-xl(1))/range(xl);
yShift = (rectY(1)-yl(1))/range(yl);
xlim(ax, rectX)
ylim(ax, rectY)
ax.Position(3) = axPos(3)*xScale;
ax.Position(4) = axPos(4)*yScale;
ax.Position(1) = axPos(1) + xShift;
ax.Position(2) = axPos(2) + yShift;
title('Maintain region''s area within the figure')
  5 件のコメント
Adam Danz
Adam Danz 2020 年 12 月 3 日
Socially-distant high-five! 🖐
Antonio Spera
Antonio Spera 2020 年 12 月 3 日
🖐

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime-Frequency Analysis についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by