Matlab代码到Python代码的转换(使用数据帧)

2024-06-26 14:26:19 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在将Matlab代码改为python代码,用于可视化图形。此代码基本上用于处理来自雷达信号的数据。这是Matlab代码。你知道吗

data1 = csvread('degree45_rd1_sitstill_0305.csv');  
rd1_data = data1(:,2:end);
rd1_nor = abs(rd1_data);
radarResolution = 0.0522; 
radarDataTimes = data1(:,1);
% plot raw data
fs = floor((length(radarDataTimes)-1)/(radarDataTimes(end)- radarDataTimes(1)));
ts = 1/fs;
t = (0:length(rd1_nor(:,1))-1)/fs;
dist_rd1 = (0:length(rd1_nor(1,:))-1)*radarResolution;
figure;
contour(dist_rd1,t,rd1_nor);
title('Radar 1 raw data plot'); ylabel('Time (s)'); xlabel('Distance (m)');

下面是Python中的更改代码,我怀疑我是否得到了fs正确的结果,或者应用循环是正确的还是错误的。你知道吗

import pandas as pd 
import numpy as np 
import numpy
import matplotlib
matplotlib.use('Qt4Agg')

print(df)

rd1_data =df.iloc[:, 1:]
print(rd1_data)
print(np.shape(rd1_data))

a = numpy.asarray(rd1_data)
print(np.shape(a))

arr = []

i = 0
for v in a:
arr.append([])
for x in v:
arr[i].append(abs(complex(x)))
i+=1
print(arr)


data = pd.DataFrame(arr)
print(data)



radar_datatime = df.iloc[:,0]

print(radar_datatime)
import math
radarResolution = 0.0522
fs = math.floor(len(radar_datatime)-1)/(radar_datatime[180]-radar_datatime[0])
print(fs)
ts = 1/fs
print(ts)


print(np.shape(arr))


t = [((val[0]-1)/fs) for val in arr]
print(t)
print(np.shape(t))
dist_rd1 = [((val-1)*radarResolution) for val in arr[0]]
print(dist_rd1)
print(np.shape(dist_rd1))

data_t=data.iloc[:,:]
print(data_t)





import matplotlib.pyplot as plt
plt.contour(dist_rd1,t,arr)
plt.savefig("/Users/fateh/Documents/my_file.png")
plt.show()

为了验证我是否正确地编辑了python代码。我们需要找到最好的解决方案。提前谢谢。你知道吗


Tags: 代码importdatadistnpfsprintarr
1条回答
网友
1楼 · 发布于 2024-06-26 14:26:19

我觉得你把事情搞得太复杂了。 我不确定你的原始数据文件看起来像什么,我已经做了一个合成图来测试你的代码了。我不完全确定你在这里要做什么手术。你知道吗

我已经为您翻译了Matlab代码。检查正在进行的操作,特别是t的计算(除非你真的想/通过ts而不是像例子中那样的fs)。你知道吗

所以我不确定这正是你想要的,但它确实起了作用!你知道吗

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#data1 = pd.readcsv('degree45_rd1_sitstill_0305.csv') 
#Synthetic Data:
data1=pd.DataFrame(np.random.randint(1000,size=(5,10)))

rd1_data = data1.iloc[:,1:-1]
#Changed to 1:-1 as index in Matlab stat at 1 compared to 0 in python
rd1_nor = abs(rd1_data)
radarResolution = 0.0522
radarDataTimes = data1.iloc[:,0]

# plot raw data
fs = np.floor((len(radarDataTimes)-1)/(radarDataTimes.iloc[-1]- radarDataTimes[0]))
ts = 1/fs

t = len((rd1_nor.iloc[:,1])-1)
t=np.arange(0,t) #/ts
#/fs doesn't want to work. Not sure if its something with my synthetic dataset though or if want to / fs or ts.

dist_rd1 = len((rd1_nor.iloc[1,:])-1)
dist_rd1=np.arange(0,dist_rd1)*radarResolution

plt.contourf(dist_rd1,t,rd1_nor);
plt.title('Radar 1 raw data plot')
plt.ylabel('Time (s)')
plt.xlabel('Distance (m)')

相关问题 更多 >