0
$\begingroup$

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  
load /dir/t11.txt  
load /dir/a11.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);
```
$\endgroup$
0
$\begingroup$
# 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  
# load /dir/t11.txt  
# load /dir/a11.txt  
## yow need to save the result in variables
k11 = np.loadtxt('/dir/k11.txt')
t11 = np.loadtxt('/dir/t11.txt')
a11 = np.loadtxt('/dir/a11.txt')

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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