Using MATLAB, I can import an audio signal and use some FFT function to get the spectrogram into the workspace. My question from here, is how to then process this spectrogram with image filtering techniques that usually import some image. For example, here is the code that filters an image:

J = imread('example.jpg');
K = somefilter(J);

my question is the imread part; as you can see the image is imported to J and then this J is used for the processing function below. However, can I simply input my spectrogram data from the workspace directly into the filter argument? or is there some other step that needs to happen before?

Also, most image filtering matlab codes mention sometime the input is grayscale image or RGB, color, etc. My question is: how is a spectrogram in Matlab classified as? does it matter for the filter? do I need to convert the spectrogram then?

  • $\begingroup$ Despite the fact that I answered, I don't think this question is related to Signal Processing and thus I think it should rather be on stackoverflow/matlab's forum $\endgroup$
    – Florent
    Aug 2, 2017 at 3:14
  • $\begingroup$ It is related to signal processing because I am processing an audio signal using image filtering techniques. The thing is, I think I need to maybe rephrase my question: can image filtering techniques be applied directly to spectrogram data? or, do I need to first convert the spectrogram to an image? but then I would need the inverse process as well, because I want to recover the time domain signals. $\endgroup$
    – Dan
    Aug 2, 2017 at 3:56
  • $\begingroup$ No matter the context, it's still a language-specific question... And you didn't give enough information on what type of filtering you want to make, what's the purpose of all that, why can't you just filter the FFT data etc. $\endgroup$
    – Florent
    Aug 2, 2017 at 4:02
  • $\begingroup$ What you mean by "language-specific" question? You mean MATLAB language? But why would that matter? Assuming we are dealing with signals (images, audio, video, etc) and you want to process them with some technique, the question remains the same, I just used MATLAB as an example. I didn't give information on type of filtering, because the specific filtering doesn't matter, it's the main question that matters: is it possible to process spectrogram data directly using some image filtering technique, since they usually operate on real images. I am not sure what you don't understand. But thanks. $\endgroup$
    – Dan
    Aug 2, 2017 at 4:42
  • $\begingroup$ Well it is indeed possible that I don't understand your question. It is also possible that your question isn't very clear. The first shady element is what you mean by "spectrogram data". Assuming you're talking about Matlab's specgram function outputs, it's a matrix where time increases across the columns and frequency down the rows, starting from zero. Considering that an image for matlab is nothing else than a $M\times N$ matrice, yeah filtering it will work. Will it make sense? I don't know, it depends on your filtering type and your application, so I guess it matters after all... $\endgroup$
    – Florent
    Aug 2, 2017 at 4:57

2 Answers 2


A spectrogram of a monochannel (real) audio file would generally be a complex 2D matrix, hence a sort of complex image. So you can use 2D filters directly on the complex matrix in the workspace. However, filtering complex data can be complicated, due to the non-linear coupling between the phase and the magnitude: if you want to preserve the Hermitian properties, which is useful when you want to invert the spectrogram into a real (filtered) signal. So traditional algorithms like enhancement, shrinkage, adaptive filtering, masking are possible.

Thus, no need to go through an intermediate binary format like:

J = imread('example.jpg');

that will quantize or compress values and somehow loose the complex aspect.

Note that processing complex values requires some care. In many situations, people only want to analyze the magnitude of the spectrogram, which then become a mere positive real-valued 2D matrix. Then, all sorts of 2D processing are available. In that case, you should take care of preserving the positivity. Most linear filters with non-negative coefficients will do the job. Non-linear processing tools based on histograms, on ranking, may require a simplification of the floating-point values.

  • 1
    $\begingroup$ Thank you, this sort of answers my question and a little bit more with extra information! Thank you! $\endgroup$
    – Dan
    Aug 2, 2017 at 18:08
  • $\begingroup$ Perfect, basically your question often a lot of answers. Phase/amplitude balance is a tough question, depending on signals $\endgroup$ Aug 2, 2017 at 18:59

First solution

A trivial solution is to save your plot to an image (JPG for example) through Matlab's GUI like so : Matlab export to jpg

And then to import it in the same way that you usually do, with maybe some cropping midway.

Second (and better) solution

You can directly do that in your code with Matlab's getframe and frame2im functions.

Here is a little explanatory code :

F = getframe
A = frame2im(F)



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.