Visualise/compare numpy arrays from Matlab/Octave to matplotlib

I'm new to python and matplotlib, and I'd like to visualise / compare 3 mfcc files stored as numpy arrays in txt format.
I have the Octave code below, and I'd like to know how it can be done using python/matplotlib.

Any help is much appreciated.

load /dir/k11.txt

subplot(1,2,1);imagesc(j11);axis('xy');colormap(jet);colorbar;subplot(1,2,2);imagesc(t11);axis('xy');colormap(jet);colorbar;

c=[k11(:,end),k11(:,1:end-1)];
figure(1);
Ncep=size(c,2)-1;
a=real(fft([c,zeros(size(c,1),512-Ncep*2-1),c(:,end:-1:2)]'));
imagesc(a(1:end/2,:));
axis('xy');
colormap(jet);

c=t11;
figure(2);
Ncep=size(c,2)-1;
a=real(fft([c,zeros(size(c,1),512-Ncep*2-1),c(:,end:-1:2)]'));
imagesc(a(1:end/2,:));
axis('xy');
colormap(jet);

c=a11;
figure(3);
Ncep=size(c,2)-1;
a=real(fft([c,zeros(size(c,1),512-Ncep*2-1),c(:,end:-1:2)]'));
imagesc(a(1:end/2,:));
axis('xy');
colormap(jet);
$$$$


# in python you will need to import the modules you use
# some users with matlab/octave background prefer
# to use from numpy import *, making all the functions global
# but with the time they learn (some not) that modules are good.
# you prevent overwriting an internal function with a custom function
# for instance
import numpy as np;

# for plotting we use matplotlib
import matplotlib.pyplot as plt

# load /dir/k11.txt
## yow need to save the result in variables


Matplotlib will provide basically the same functions

plt.subplot(1,2,1);
plt.imshow(j11); # or you can use pcolormap
plt.set_colormap('jet')
plt.colorbar()
plt.subplot(1,2,2)
plt.imshow(t11);
plt.set_colormap('jet')
plt.colorbar()


Unfortunately python does not have a way to build block matrices, if you want to do vertical concatenation you can simply build an array from a list of arrays. Also instead of using the keyword end you simply use negative indices, indices count from 0 not from 1, and you use square brackets to differentiate indexing from function calls, and you can omit an index when using a slice ending at the end of the axis.

#c=[k11(:,end),k11(:,1:end-1)];
c = np.hstack([k11[:, -1], k11[:, :-1]])


For the above operation you could use np.roll instead.

plt.figure() # I don't use figure identifiers probably you can
Ncep=c.shape[1]-1;

# the step size in python is the third so x[::-1] is flipping x.
# by default fft operats on the last axis (axis=-1)
# if I remember correctly matlab by default
# will apply fft on the columns (axis=0)
a=np.real(np.fft.fft(np.hstack([c,np.zeros((c.shape[0],512-Ncep*2-1)),c[:, :0:-1]]), axis=0);


I think with this you already got started, in the module np.fft you will find routines like real fft (fftr, ifftr), complex fft (fft, ifft), fftshift, fftfreq`. And you find documentation here

• That's a nice point to build on. Thank you Feb 25 at 19:25
• YOu are wellcome, dsp.stackexchange.com/tour make sure to wrap wen satisfied.
– Bob
Feb 26 at 5:44