I'm analyzing some sound clips using the spectrogram() function in MATLAB. I would like to save the spectrogram as an image (jpg, png, etc). But regardless of what image format I save the figure in, the resulting image always looks different ("spotty") from what I see in the figure.

Here's an example of the spectrograms: Matlab Figure vs. Saved Image

All I want is to save exactly what I see in the figure as an image. I've already tried saving the figure in all the image formats possible but all of them are producing the same "spotting" effect. I've also tried both manual saving (click on file -> save as) and programmatically using the print() and the saveas() functions. Same result every time.

Any help would be appreciated!

enter image description here

有帮助吗?

解决方案 2

I found a work-around for this problem by using the pcolor() function, which is essentially a rotated surf() function plotted in a grid format (doc). After tinkering with the spectrogram() function more, I'm convinced that these "spotting" artifacts have nothing to do with the data format, property, or scale. The problem seems to lie in the way MATLAB plots and visualizes 3D plots. I tried plotting with the mesh() function as well and it produced a different kind of "spotting" effect. pcolor() works because it's a 2D visualization of a 3D plot.

This is how spectrogram() plots the image using surf() (adapted from the doc):

[S,T,F,P] = spectrogram(X,256,250,256,2000);
surf(T,F,abs(S),'EdgeColor','none');
axis tight; view(0,90);

... and this is how to use pcolor() to plot a save-friendly image:

[S,T,F,P] = spectrogram(X,256,250,256,2000);
h = pcolor(T,F,abs(S));
set(h,'EdgeColor','none');

其他提示

What is the data range of your spectrogram? One of reasons might be that your spectrogram range is out of the [0,1] region for double images or [0,255] for uint* images (your white spots on saved image are suspiciously close to the local minima on MatLab figure).

Another guess might be that you are using imwrite function, in particular its imwrite(X,map,filename,fmt) syntax. MatLab documentation explains:

imwrite(X,map,filename,fmt) writes the indexed image in X and its associated colormap map to filename in the format specified by fmt. If X is of class uint8 or uint16, imwrite writes the actual values in the array to the file. If X is of class double, imwrite offsets the values in the array before writing, using uint8(X–1). map must be a valid MATLAB colormap. Note that most image file formats do not support colormaps with more than 256 entries.

so the uint8(X–1) might be the source of the white spots. Though have no idea why they appear after print()'ing.

The white spots are an OpenGL issue, which is the renderer used in spectrogram()'s internal call to surf(). Since you are interested in plotting a 2D visualization, change the renderer for the current figure to zbuffer:

set(gcf, 'renderer', 'zbuffer');

where gcf means "get current figure". The white spots are now gone.

Note that you can also select the zbuffer renderer when you create the figure, before calling spectrogram():

myNewFig = figure('renderer','zbuffer');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top