# how to use image filters for spectrograms?

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?

• 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 Aug 2 '17 at 3:14
• 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.
– Dan
Aug 2 '17 at 3:56
• 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. Aug 2 '17 at 4:02
• 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.
– Dan
Aug 2 '17 at 4:42
• 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... Aug 2 '17 at 4:57

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:

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.

• Thank you, this sort of answers my question and a little bit more with extra information! Thank you!
– Dan
Aug 2 '17 at 18:08
• Perfect, basically your question often a lot of answers. Phase/amplitude balance is a tough question, depending on signals Aug 2 '17 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 :

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 :

plot(rand(5))
F = getframe
A = frame2im(F)
imshow(A)


Source