How to plot contour of FFT of an image in Matlab - matlab

I want to know how to plot the contour of the FFT of an image in Matlab. I have this code but when plotting the contour I get a blue plot. I think I need to specify the range of the frequencies in the contour function, but how to know/compute the range?
monolayer = double(imread('TEM_monolayer_graphene.bmp'));
monolayerFFTs = fftshift(fft2(monolayer));
contour(monolayerFFTs);
I think instead of the blue plot I should get a 3D plot with some spikes at the frequencies where there is more energy.

I was able to get a really good plot with the following code
monolayer = double(imread('TEM_monolayer_graphene.bmp'));
monolayerFFTs = fftshift(fft2(monolayer));
contour(abs(monolayerFFTs));

Related

overlaying isolines on contourf plot

I would like to be able to overlay isolines over a filled contour or surface as it is done in this figure:
Can matlab overlay contour and contourf plots?
So far, I have tried this:
[X,Y] = meshgrid(x_cases,y_cases);
Points = length(x_cases)*length(y_cases);
resX = reshape(X,Points,1);
resY = reshape(Y,Points,1);
resZ = reshape(DataGrid_a,Points,1);
scatter(resX,resY,[],resZ,’filled’)
hold on
contour(X,Y,DataGrid_b,'ShowText','on')
But I have to reduce the transparency of my scatter plot to be able to see the contour lines from DataGrid_b, it would be more ideal to not change the transparency and overlay my isolines. I appreciate any input you may give me!
Thanks!
The simplest solution (and it's very hack) is to take advantage of the fact that 2D plots are plotted at Z = 0; So put your scatter points at some Z value below that.
scatter3(resX,resY,-ones(size(resX)),[],resZ,’filled’)
view(2)
hold on
contour(X,Y,DataGrid_b,'ShowText','on')

What exactly does Matlab do to plot a spectrogram?

I'm trying to plot a spectrogram of a vibration signal using Matlab, but I'm not happy with the way that the spectrogram-function plots the signal (I would like to customize the axes and use a mapped vector instead of time). Right now I'm trying to plot the thing myself using pcolor:
[M_s, M_w, M_t] = spectrogram(M_i, 2^16, ceil(2^16*0.95), [0:0.2:15000], Sample_rate);
figure;
[M_t, M_w] = meshgrid(M_t, M_w);
h = pcolor(M_t, M_w, mag2db(abs(M_s)));
set(h, 'LineStyle', 'none);
However, doing this, my plotted figure is extremely slow and I can't really work with that, because it takes ages to zoom in or do anything with it. When I just use spectrogram to plot the figure, it's super fast. So my question is, what exactly does spectrogram do, to plot the figure?

How to plot a density graph in Matlab

I have a matrix of size 2000 by 300. When I try to plot a 3D graph using surf, it is very dark because of the amount of points. I tried different colors but it is still very dark. How do I plot the density of the graph instead of the 3D? Also, the original matrix was about 3000 times bigger than this (I had to decimate it to get to 2000x300). Do I need another kind of plot for the original one or does density plot work?
Thank you,
Try using mesh instead of surf, here is a sample code:
clear; close; clc;
A = rand(2000,300);
[xx,yy] = meshgrid(1:size(A,2),1:size(A,1));
mesh(xx,yy,A)

Create 2D Spectrogram in Matlab

I am in need of plotting a 2D spectrogram of a signal in Matlab. I need it for a printed assignment, hence the 3D image makes no sense. However, when the signal is plotted using Spectrogram it automatically produces a 3D plot of the signal.
My Code:
Dataset = 1; % Dataset to be analysed
N = 1024; % Window size
Beta = 12; % Kaiser window beta value (small = narrow main lope)
Overlap = 800; % Window overlap
Threshold = -150; % Minimum magnitude before threshold
spectrogram(Enclosure{Dataset}(1:end),kaiser(N,Beta),Overlap,2048,fs,'MinThreshold',Threshold,'yaxis');
which produces a graph that looks like this:
But it is seen from the top, and the graph is really showing this:
The reason why i need it to specifically be 2D (and why i don't settle with a screenshot) is because i am using Matlab2tikz to convert Matlab figures into Tikz figures in LaTex. with the 3D images i get figures of +100 Mb and 2D will reduce the size to <1Mb.
I don't know what version of Matlab you are using but in 2015a you should be able to get a handle to the figure with the 3D plot and change the view angle to 2D:
view(0,90);
I've also got an example of how you can make your own 2D plot from the outputs of spectrogram() using a similar method:
x = [0:0.01:100];
y = sin(5*x);
y = awgn(y,0.1);
[S,F,T,P] = spectrogram(y,200,0,length(y)*5,100);
[m,n] = size(P);
figure(2)
surf(F,T,zeros(n,m),P','EdgeColor','none')
view(0,90)
xlabel('Frequency')
ylabel('Time (s)')
The output looks like this:
Hopefully since there is no altitude information, the figure size might be smaller but I can't test that since I don't have Matlab2tikz.
One option is to capture whatever its plotted and then plot it as an image. You can do this using getframe
if you do
F=getframe(gca);
cla;
imshow(F.cdata);
You'll get exactly what you will be seeing before, but as an image.
However I think it defeats a bit the purpose of Matlab2Tikz, as the idea os that you have Tikz code describing your data...
You can try the following:
[~,F,T,ps]=spectrogram(Enclosure{Dataset}(1:end),kaiser(N,Beta),Overlap,2048,fs,'MinThreshold',Threshold,'yaxis').
% Output the spectrum in ps
imagesc(T,F,10*log10(ps))
% Generate a 2d image
view(270,90)
xlabel('Time [s]')
ylabel('Frequency [Hz]')
c=colorbar;
c.Label.String='Power [dB]';
% Extra setting to make the plot look like the spectrogram
Good luck

Matlab contour plot smooth colors

Could you tell me how to plot in Matlab figure such as below (smooth transition of colors)? Function countour allows only to create plot with contour lines which doesn't provide enough information to me.
You can use imagesc with the 'jet' colormap. Here's an example:
x = conv2( randn(600), fspecial('gaussian',200,20), 'valid'); %// example 2D smooth data
imagesc(x)
colormap(jet)
colorbar
grid
That is not a contour plot!
try imagesc, surf and all of their variants:
http://uk.mathworks.com/help/matlab/surface-and-mesh-plots-1.html
http://uk.mathworks.com/help/matlab/image-file-operations.html