[英]python convert informal complex number file to formal amplitude data
I have a txt file composed of 90 groups of carrier data, which separated by a new line. 我有一个由90组运营商数据组成的txt文件,该文件由新行分隔。 Each carrier data is composed of 200 complex numbers, which separated by tab. 每个载波数据由200个复数组成,并用制表符分隔。
what I want is just a 90*200 array, each is a amplitude of the complex number.How can I read the file and convert? 我想要的只是一个90 * 200的数组,每个都是复数的幅度。如何读取文件并进行转换?
Did you mean that? 你是那个意思吗
result = []
file = open('yourfile.txt')
for line in file.readlines():
result.append(line.split())
file.close()
print(result)
You can use read_csv
with parameter skip_blank_lines
for getting groups: 您可以将read_csv
与参数skip_blank_lines
来获取组:
import pandas as pd
import numpy as np
from pandas.compat import StringIO
temp=u"""
7+3j;2+1j
1+6j;5+1j
8+3j;1+7j
5+4j;4+1j
6+2j;2+1j
8+4j;9+3j
"""
#after testing replace StringIO(temp) to filename
df = pd.read_csv(StringIO(temp),
sep=";", #in real data use sep='\t'
skip_blank_lines=False,
names=np.arange(2)) #in real data use 200
print (df)
0 1
0 NaN NaN
1 7+3j 2+1j
2 1+6j 5+1j
3 NaN NaN
4 8+3j 1+7j
5 5+4j 4+1j
6 NaN NaN
7 6+2j 2+1j
8 8+4j 9+3j
Create index by selecting first column by iloc
, get mask by isnull
and then apply cumsum
- create groups with same values in index
: 通过选择第一列创建索引iloc
,通过获得面膜isnull
,然后应用cumsum
-创建具有相同值的组index
:
df.index = df.iloc[:, 0].isnull().cumsum()
Then delete all rows where NaN
in first column by dropna
: 然后通过dropna
删除第一列中NaN
所有行:
df = df.dropna(subset=[0])
Convert to complex and get amplitude by numpy.angle
: 转换为复数并通过numpy.angle
获得振幅:
df = df.applymap(lambda x: np.angle(np.complex(x)))
print (df)
0 1
0
1 0.404892 0.463648
1 1.405648 0.197396
2 0.358771 1.428899
2 0.674741 0.244979
3 0.321751 0.463648
3 0.463648 0.321751
Last groupby
by index and convert to numpy array by values
: 最后按索引groupby
,并按values
转换为numpy数组:
print (df.groupby(level=0).apply(lambda x: x.values).values)
[array([[ 0.40489179, 0.46364761],
[ 1.40564765, 0.19739556]])
array([[ 0.35877067, 1.42889927],
[ 0.67474094, 0.24497866]])
array([[ 0.32175055, 0.46364761],
[ 0.46364761, 0.32175055]])]
If dont need groups use: 如果不需要组使用:
import pandas as pd
import numpy as np
from pandas.compat import StringIO
temp=u"""
7+3j;2+1j
1+6j;5+1j
8+3j;1+7j
5+4j;4+1j
6+2j;2+1j
8+4j;9+3j
"""
#after testing replace StringIO(temp) to filename
df = pd.read_csv(StringIO(temp),
sep=";", #in real data use sep='\t'
names=np.arange(2)) #in real data use 200
print (df)
0 1
0 7+3j 2+1j
1 1+6j 5+1j
2 8+3j 1+7j
3 5+4j 4+1j
4 6+2j 2+1j
5 8+4j 9+3j
df = df.applymap(lambda x: np.angle(np.complex(x)))
print (df)
0 1
0 0.404892 0.463648
1 1.405648 0.197396
2 0.358771 1.428899
3 0.674741 0.244979
4 0.321751 0.463648
5 0.463648 0.321751
print (df.values)
[[ 0.40489179 0.46364761]
[ 1.40564765 0.19739556]
[ 0.35877067 1.42889927]
[ 0.67474094 0.24497866]
[ 0.32175055 0.46364761]
[ 0.46364761 0.32175055]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.