我正在尝试编写一个调制函数,参考下面的函数。我不是电气工程师,对信号处理了解不多。
private final int SAMPLES_PER_SYMBOL = 150
private final float NFREQ = 1/15
private float[] bytes2signal(byte[] buf) {
float[] signal = new float[buf.length * 8 * SAMPLES_PER_SYMBOL * 2] // 8 bits/byte, 2 floats/sample
int p = 0
for (int i = 0; i < buf.length; i++) {
for (int j = 0; j < 8; j++) {
int bit = (buf[i] >> j) & 0x01
float f = bit == 1 ? -NFREQ : NFREQ
for (int k = 0; k < SAMPLES_PER_SYMBOL; k++) {
signal[p++] = (float)Math.cos(2 * Math.PI * f * k)
signal[p++] = (float)Math.sin(2 * Math.PI * f * k)
}
}
}
return signal
}
但我的缓冲区信号实际上是非二进制数据(这是扩展信号),即我的 buf 数组的第一个元素是-1,0,0,-1,0,1,1,0
如何使用此调制功能调制我的扩展信号?我对for
循环中发生的事情有点困惑。
为了更好地理解这一点,我对数据进行了静态转换,请参阅下文。
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
//JAVA BYTE ARRAY DECLARATION
byte Bytesdata[];
//MEMORY ALLOCATION FOR JAVA BYTE ARRAY
Bytesdata = new byte[4];
//ASSIGNING ELEMENTS TO JAVA BYTE ARRAY
Bytesdata[0] = 20;
Bytesdata[1] = 10;
Bytesdata[2] = 30;
Bytesdata[3] = 5;
//BYTE ARRAY OUTPUT
int sourcestation = 2;
int[][] wtable = new int[][]{
{ 1, 1, 1, 1, 1, 1, 1, 1},
{ 1, -1, 1, -1, -1, 1, 1, -1},
{ -1, -1, 1, -1, 1, -1, -1, 1},
{ 1, -1, 1, -1, -1, 1, 1, -1},
{ 1, 1, -1, 1, -1, 1, -1, -1},
{ 1, -1, 1, -1, -1, 1, 1, -1},
{ -1, -1, 1, -1, -1, -1, 1, 1},
{ 1, -1, 1, 1, -1, -1, 1, 1},
};
int[][] results = new int[wtable.length][wtable[0].length];
int pos = 0;
System.out.println(Bytesdata.length);
for (int i = 0; i < Bytesdata.length; i++) {
System.out.println(Bytesdata[i]);
System.out.println("Binary is " + (Integer.toBinaryString(Bytesdata[i])) );
System.out.println("Byte is " + getByte(Bytesdata[i]));
int j = 0;
while (j < wtable.length) {
System.out.println("j: "+j);
results[i][j] = wtable[sourcestation][j] * getBit(Bytesdata, pos);
System.out.println("wtable["+sourcestation+"]["+j+"]: "+wtable[sourcestation][j]);
System.out.println("Position: "+pos);
System.out.println("Data Bit: "+getBit(Bytesdata, pos));
System.out.println("Result: "+results[i][j]);
System.out.println("---------------------------");
j+=1;
pos+=1;
}
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(results[i][j]+", ");
}
System.out.print("\n");
}
final int HDRSIZE = 5;
final int SAMPLES_PER_SYMBOL = 150;
final float NFREQ = 1/15;
float[] signal = new float[Bytesdata.length * 8 * SAMPLES_PER_SYMBOL * 2];
int p = 0;
for (int i = 0; i < Bytesdata.length; i++) {
for (int j = 0; j < 8; j++) {
int bit = (Bytesdata[i] >> j) & 0x01;
float f = bit == 1 ? -NFREQ : NFREQ;
for (int k = 0; k < SAMPLES_PER_SYMBOL; k++) {
signal[p++] = (float)Math.cos(2 * Math.PI * f * k);
signal[p++] = (float)Math.sin(2 * Math.PI * f * k);
}
}
}
System.out.println(signal);
// System.out.println("Binary is " + (Integer.toBinaryString(signal)));
}
private static int getBit(byte[] data, int pos) {
int posByte = pos/8;
int posBit = pos%8;
byte valByte = data[posByte];
int valInt = valByte>>(8-(posBit+1)) & 0x0001;
return valInt;
}
private static String getByte(Byte b){
String temp = Integer.toBinaryString(b);
while(temp.length() < 8){
temp = "0" + temp;
}
return temp;
}
}
我认为我的输出似乎是正确的。
Hello World
4
20
Binary is 10100
Byte is 00010100
j: 0
wtable[2][0]: -1
Position: 0
Data Bit: 0
Result: 0
---------------------------
j: 1
wtable[2][1]: -1
Position: 1
Data Bit: 0
Result: 0
---------------------------
j: 2
wtable[2][2]: 1
Position: 2
Data Bit: 0
Result: 0
---------------------------
j: 3
wtable[2][3]: -1
Position: 3
Data Bit: 1
Result: -1
---------------------------
j: 4
wtable[2][4]: 1
Position: 4
Data Bit: 0
Result: 0
---------------------------
j: 5
wtable[2][5]: -1
Position: 5
Data Bit: 1
Result: -1
---------------------------
j: 6
wtable[2][6]: -1
Position: 6
Data Bit: 0
Result: 0
---------------------------
j: 7
wtable[2][7]: 1
Position: 7
Data Bit: 0
Result: 0
---------------------------
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
10
Binary is 1010
Byte is 00001010
j: 0
wtable[2][0]: -1
Position: 8
Data Bit: 0
Result: 0
---------------------------
j: 1
wtable[2][1]: -1
Position: 9
Data Bit: 0
Result: 0
---------------------------
j: 2
wtable[2][2]: 1
Position: 10
Data Bit: 0
Result: 0
---------------------------
j: 3
wtable[2][3]: -1
Position: 11
Data Bit: 0
Result: 0
---------------------------
j: 4
wtable[2][4]: 1
Position: 12
Data Bit: 1
Result: 1
---------------------------
j: 5
wtable[2][5]: -1
Position: 13
Data Bit: 0
Result: 0
---------------------------
j: 6
wtable[2][6]: -1
Position: 14
Data Bit: 1
Result: -1
---------------------------
j: 7
wtable[2][7]: 1
Position: 15
Data Bit: 0
Result: 0
---------------------------
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
30
Binary is 11110
Byte is 00011110
j: 0
wtable[2][0]: -1
Position: 16
Data Bit: 0
Result: 0
---------------------------
j: 1
wtable[2][1]: -1
Position: 17
Data Bit: 0
Result: 0
---------------------------
j: 2
wtable[2][2]: 1
Position: 18
Data Bit: 0
Result: 0
---------------------------
j: 3
wtable[2][3]: -1
Position: 19
Data Bit: 1
Result: -1
---------------------------
j: 4
wtable[2][4]: 1
Position: 20
Data Bit: 1
Result: 1
---------------------------
j: 5
wtable[2][5]: -1
Position: 21
Data Bit: 1
Result: -1
---------------------------
j: 6
wtable[2][6]: -1
Position: 22
Data Bit: 1
Result: -1
---------------------------
j: 7
wtable[2][7]: 1
Position: 23
Data Bit: 0
Result: 0
---------------------------
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
5
Binary is 101
Byte is 00000101
j: 0
wtable[2][0]: -1
Position: 24
Data Bit: 0
Result: 0
---------------------------
j: 1
wtable[2][1]: -1
Position: 25
Data Bit: 0
Result: 0
---------------------------
j: 2
wtable[2][2]: 1
Position: 26
Data Bit: 0
Result: 0
---------------------------
j: 3
wtable[2][3]: -1
Position: 27
Data Bit: 0
Result: 0
---------------------------
j: 4
wtable[2][4]: 1
Position: 28
Data Bit: 0
Result: 0
---------------------------
j: 5
wtable[2][5]: -1
Position: 29
Data Bit: 1
Result: -1
---------------------------
j: 6
wtable[2][6]: -1
Position: 30
Data Bit: 0
Result: 0
---------------------------
j: 7
wtable[2][7]: 1
Position: 31
Data Bit: 1
Result: 1
---------------------------
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
0, 0, 0, -1, 0, -1, 0, 0,
0, 0, 0, 0, 1, 0, -1, 0,
0, 0, 0, -1, 1, -1, -1, 0,
0, 0, 0, 0, 0, -1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
[F@6d06d69c
在原始modulation
函数中,有基于 的决策1 or 0
。我怎样才能调制我的信号?
0, 0, 0, -1, 0, -1, 0, 0,
0, 0, 0, 0, 1, 0, -1, 0,
0, 0, 0, -1, 1, -1, -1, 0,
0, 0, 0, 0, 0, -1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
难道我做错了什么 ?我怎样才能动态地做乘法element-by-element
?