将 MATLAB 代码转换为 Python 并收到 TypeError:'numpy.ndarray'对象不可调用错误



我正在将生成波形的MATLAB代码转换为Python。就上下文而言,这是来自原子力显微镜的带激发响应的模拟(与代码错误无关(。这是MATLAB代码

%simulate BE response over a line scan
% define experimental parameters
IO_rate = 4E6; %[samples/sec]
N_pixels = 128; % number of pixels along a line scan
N_points_per_pixel = 2^13; % number of data points per pixel
w1 = 200E3; % lower edge of band
w2 = 400E3; % upper edge of band
noise_level = .1; %add noise to the signal
w_vec = -IO_rate/2: IO_rate/N_points_per_pixel : IO_rate/2-IO_rate/N_points_per_pixel; %frequency vector over a pixel
% vary A, wo, Q, and phase over pixels
p_vec = (0:N_pixels-1)/N_pixels;
A_vec = sin(2*pi*3*p_vec)+2; %amplitude
wo_vec = 250E3 + 100E3*p_vec; %resonance
Q_vec = 100 - 50*p_vec; % Q-factor
phi_vec = sign(p_vec-.5); % phase
% build drive signal, define in the Fourier domain
D_vec = zeros(size(w_vec));
D_vec( ((abs(w_vec)<w2) + (abs(w_vec)>w1)) == 2 ) = 1; % drive bins located within upper and lower band edges
band_ind = find( (((w_vec)<w2) + ((w_vec)>w1)) == 2 );
d_vec = fftshift(ifft(ifftshift(D_vec))); % find drive signal in the time domain
% build response at each pixel
R_mat = zeros(N_pixels,N_points_per_pixel);
r_mat = zeros(N_pixels,N_points_per_pixel);
for k1 = 1 : N_pixels
H_vec = (A_vec(k1).*wo_vec(k1).^2).*exp(1i*phi_vec(k1))./(w_vec.^2 + 1i*wo_vec(k1)*w_vec/Q_vec(k1) - wo_vec(k1).^2); %cantilever transfer function
R_mat(k1,:) = (H_vec.*D_vec); %response of the cantilever in the Fourier domain

%determine response in the time domain (this is a little hokey, but it should work for simulation)    
r_mat(k1,:) = fliplr((real((ifft(fftshift(R_mat(k1,:)))))));    
end
% build full response in the time domain;
r_vec = reshape(r_mat.',[ 1 N_pixels*N_points_per_pixel]);
% add noise
r_vec = r_vec + noise_level*2*(rand(size(r_vec))-.5);
%save response as a .mat (which can be read into python if needed)

以下是到目前为止我将其转换为python代码的方法

#simulate BE response over a line scan
# define experimental parameters
IO_rate = 4E6; #[samples/sec]
N_pixels = 128; # number of pixels along a line scan
N_points_per_pixel = 8192; # number of data points per pixel
w1 = 200E3; # lower edge of band
w2 = 400E3; # upper edge of band
noise_level = .1; #add noise to the signal
w_vec = np.arange(-IO_rate/2, IO_rate/2-IO_rate/N_points_per_pixel + 1, IO_rate/N_points_per_pixel)
# vary A, wo, Q, and phase over pixels
p_vec = np.arange(0, N_pixels-1)/N_pixels
A_vec = np.sin(2*np.pi*3*p_vec)+2 #amplitude
wo_vec = 250E3 + 100E3*p_vec #resonance
Q_vec = 100 - 50*p_vec # Q-factor
phi_vec = np.sign(p_vec-.5) # phase
D_vec = np.zeros(np.size(w_vec))
ind = (abs(w_vec)<w2) & (abs(w_vec)>w1);
D_vec[ind] = 1; #assign those indices to 1.
band_ind = np.nonzero(((w_vec)<w2) & ((w_vec)>w1));
d_vec = np.fft.fftshift(np.fft.ifft(np.fft.ifftshift(D_vec))) #find drive signal in the time domain
R_mat = np.zeros((N_pixels,N_points_per_pixel))
r_mat = np.zeros((N_pixels,N_points_per_pixel))
for k1 in range(N_pixels):
H_vec = ((A_vec(k1)*wo_vec(k1)**2)*np.exp(1j*phi_vec(k1))/(w_vec**2 + 1j*wo_vec(k1)*w_vec/Q_vec(k1) - wo_vec(k1)**2)); #cantilever transfer function

在执行了for循环中迄今为止的内容后,我得到了TypeError: 'numpy.ndarray' object is not callable,所以我不确定我做错了什么?

问题存在于循环中向量的索引中
代码应为:

H_vec = ((A_vec[k1]*wo_vec[k1]**2)*np.exp(1j*phi_vec[k1])/(w_vec**2 + 1j*wo_vec[k1]*w_vec/Q_vec[k1] - wo_vec[k1]**2)); #cantilever transfer function

循环中似乎也有一个问题。你是不是想写:

for k1 in range(N_pixels-1):

  1. 原因是使用()运算符而不是[]来访问数组中的项(即使用元素引用的MatLab样式而不是Phytonian样式(
  2. 此外,在np.arrange()中,您应该传递N_pixels,否则您将得到IndexError: index 127 is out of bounds for axis 0 with size 127错误
  3. 由于Python使用缩进作为行之间的分隔,因此不需要在每行的末尾添加;

以下是更正后的版本:

import numpy as np
#simulate BE response over a line scan
# define experimental parameters
IO_rate = 4E6 #[samples/sec]
N_pixels = 128 # number of pixels along a line scan
N_points_per_pixel = 8192 # number of data points per pixel
w1 = 200E3 # lower edge of band
w2 = 400E3 # upper edge of band
noise_level = .1 #add noise to the signal
w_vec = np.arange(-IO_rate/2, IO_rate/2-IO_rate/N_points_per_pixel + 1, 
IO_rate/N_points_per_pixel)
# vary A, wo, Q, and phase over pixels
p_vec = np.arange(N_pixels)/N_pixels
A_vec = np.sin(2*np.pi*3*p_vec)+2 #amplitude
wo_vec = 250E3 + 100E3*p_vec #resonance
Q_vec = 100 - 50*p_vec # Q-factor
phi_vec = np.sign(p_vec-.5) # phase
D_vec = np.zeros(np.size(w_vec))
ind = (abs(w_vec)<w2) & (abs(w_vec)>w1)
D_vec[ind] = 1; #assign those indices to 1.
band_ind = np.nonzero(((w_vec)<w2) & ((w_vec)>w1))
d_vec = np.fft.fftshift(np.fft.ifft(np.fft.ifftshift(D_vec))) #find drive signal in the time domain
R_mat = np.zeros((N_pixels,N_points_per_pixel))
r_mat = np.zeros((N_pixels,N_points_per_pixel))
for k1 in range(N_pixels):
H_vec = ((A_vec[k1]*wo_vec[k1]**2)*np.exp(1j*phi_vec[k1])/(w_vec**2 + 1j*wo_vec[k1]*w_vec/Q_vec[k1] - wo_vec[k1]**2)) #cantilever transfer function

干杯。

相关内容

  • 没有找到相关文章

最新更新