北斗编码实现

本文根据北斗格网编码标注编写的测试代码, 北斗国标描述网址 http://c.gb688.cn/bzgk/gb/showGb?type=online&hcno=77B7EA113926D3247F9688324D4A91C8

我将北斗编码整理成一张图, 如下(代码在文末附上):

#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iosfwd>
#include <string>
#include <map>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <math.h>

std::string GBBeiDouCode(int level, double x, double y, double z)
{
	//if(y >=88)
	std::vector<std::string>  code;
	std::vector<std::string>  hcode;

	LocType ltype = XYLocType(x, y);
	std::pair<int, int> sym = LocTypeSym(ltype);

	int pre_r, pre_c;
	double posLon = -180.00000000, posLat = 0.00000000;
	if (x >= 180.00000)
		x = x - D3600_2_8888 / 100;
	if (x <= -180.000000)
		x = x - D3600_2_8888 / -100;
	//行列号按照左闭右开,最后180按照闭算到59
	for (int i = 1; i <= level; i++)
	{
		if (i == 1) {
			if (y >= 0)
				code.push_back("N");
			else
				code.push_back("S");
			int nr = floor((x - (posLon)) / 6.00000000); //0-59
			int nc = floor(fabs(y - posLat) / 4.00000000);//0-21
			std::string nrc;
			if (nr < 10)
			{
				nrc = nrc.append("0");
				int tmpnr = nr == 0 ? 1 : nr;
				nrc = nrc.append(std::to_string(tmpnr));
			}
			else
			{
				nrc = std::to_string(nr > 59 ? 60 : nr + 1);
			}
			if (nc > 21)
				nc = 21;
			char nccc = (char)(nc + 65);
			char f[2] = { nccc,0 };
			code.emplace_back();
			code[code.size() - 1].append(nrc);
			code[code.size() - 1].append(f);
			code[code.size() - 1].resize(3);
			nr = nr > 59 ? 59 : nr;
			nc = nc > 21 ? 21 : nc;
			pre_r = nr;
			pre_c = nc;

			continue;
		}
		else if (i == 2)
		{
			if (x > 0)
				posLon = posLon + (pre_r) * 6;
			else
			{
				/*	if (x <= -180.00000000000)
						posLon = posLon + (pre_r + 1) * 6;
					else*/
				posLon = posLon + (pre_r + 1) * 6;
			}
			if (y > 0)
				posLat = posLat + (pre_c) * 4;
			else
				posLat = posLat - (pre_c) * 4;

			int nc = (int)floor(fabs(y - posLat) / 0.5);//0-11
			int nr = (int)floor(fabs(x - posLon) / 0.5);//0-7
			nr = nr > 11 ? 11 : nr;
			nc = nc > 7 ? 7 : nc;
			char nrc = (char)(nr > 9 ? nr + 55 : nr + 48);
			char ncc = (char)(nc + 48);
			pre_r = nr;
			pre_c = nc;
			code.emplace_back();

			code[code.size() - 1].resize(2);
			code[code.size() - 1][0] = nrc;
			code[code.size() - 1][1] = ncc;
			continue;
		}
		else if (i == 3)
		{

			if (x > 0)
				posLon = posLon + (pre_r) * 0.5;
			else
				posLon = posLon - pre_r * 0.5;
			if (y > 0)
				posLat = posLat + (pre_c) * 0.5;
			else
				posLat = posLat - (pre_c) * 0.5;

			int nc = (int)floor(fabs(y - posLat) / D60_10);//0-2 
			int nr = (int)floor(fabs(x - posLon) / D60_15); //0-1
			nr = nr > 1 ? 1 : nr;
			nc = nc > 2 ? 2 : nc;
			pre_r = nr;
			pre_c = nc;
			int ncc = nc * 2 + nr;
			char nccc = (char)(ncc + 48);
			code.emplace_back();
			code[code.size() - 1].resize(1);
			code[code.size() - 1][0] = nccc;
			continue;

		}
		else if (i == 4)
		{
			if (x > 0)
				posLon = posLon + (pre_r)* D60_15;
			else
				posLon = posLon - (pre_r)* D60_15;
			if (y > 0)
				posLat = posLat + (pre_c)* D60_10;
			else
				posLat = posLat - (pre_c)* D60_10;

			int nc = (int)floor(fabs(y - posLat) / D60_1);
			int nr = (int)floor(fabs(x - posLon) / D60_1);
			nr = nr > 14 ? 14 : nr;
			nc = nc > 9 ? 9 : nc;
			pre_r = nr;
			pre_c = nc;
			char nrc = (char)(nr > 9 ? nr + 55 : nr + 48);
			char ncc = (char)(nc + 48);
			code.emplace_back();
			code[code.size() - 1].resize(2);
			code[code.size() - 1][0] = nrc;
			code[code.size() - 1][1] = ncc;
			continue;
		}
		else if (i == 5)
		{
			if (x > 0)
				posLon = posLon + (pre_r) * (D60_1);
			else
				posLon = posLon - (pre_r) * (D60_1);
			if (y > 0)
				posLat = posLat + (pre_c) * (D60_1);
			else
				posLat = posLat - (pre_c) * (D60_1);


			int nc = (int)floor(fabs(y - posLat) / D3600_4);//0-14
			int nr = (int)floor(fabs(x - posLon) / D3600_4);//0-14
			nr = nr > 14 ? 14 : nr;
			nc = nc > 14 ? 14 : nc;
			pre_r = nr;
			pre_c = nc;
			char nrc = (char)(nr > 9 ? nr + 55 : nr + 48);
			char ncc = (char)(nc > 9 ? nc + 55 : nc + 48);
			code.emplace_back();
			code[code.size() - 1].resize(2);
			code[code.size() - 1][0] = nrc;
			code[code.size() - 1][1] = ncc;

			continue;
		}
		else if (i == 6)
		{
			if (x > 0)
				posLon = posLon + (pre_r)* D3600_4;
			else
				posLon = posLon - (pre_r)* D3600_4;
			if (y > 0)
				posLat = posLat + (pre_c)* D3600_4;
			else
				posLat = posLat - (pre_c)* D3600_4;

			int nc = (int)floor(fabs(y - posLat) / D3600_2);//0-1
			int nr = (int)floor(fabs(x - posLon) / D3600_2);//0-1
			nr = nr > 1 ? 1 : nr;
			nc = nc > 1 ? 1 : nc;
			pre_r = nr;
			pre_c = nc;

			int ncc = nc * 2 + nr;
			char nccc = (char)(ncc + 48);
			code.emplace_back();
			code[code.size() - 1].resize(1);
			code[code.size() - 1][0] = nccc;

			continue;
		}
		else if (i >= 7 && i <= 10)
		{
			int d = i - 7 + 1;
			double dd2 = D3600_2 / pow(8, d);
			double dd1 = D3600_2 / pow(8, d - 1);
			if (x > 0)
				posLon = posLon + (pre_r)* dd1;
			else
				posLon = posLon - (pre_r)* dd1;
			if (y > 0)
				posLat = posLat + (pre_c)* dd1;
			else
				posLat = posLat - (pre_c)* dd1;

			int nc = (int)floor(fabs(y - posLat) / dd2);
			int nr = (int)floor(fabs(x - posLon) / dd2);
			nr = nr > 7 ? 7 : nr;
			nc = nc > 7 ? 7 : nc;
			pre_r = nr;
			pre_c = nc;
			char nrc = (char)(nr + 48);
			char ncc = (char)(nc + 48);
			code.emplace_back();
			code[code.size() - 1].resize(2);
			code[code.size() - 1][0] = nrc;
			code[code.size() - 1][1] = ncc;

			continue;
		}
	}

	//n0
	double r0 = 6378137.000000000;



	// a=1+sta0 b=(h+r0)/r0
	//n=sta0/sta * logb /log a b //a 为底, b为指

	double PI = 3.1415926535897932;
	double sta0 =  PI / 180.00000000;
	double sta = g_zdel[level-1]  * 0.017453292519943295;
	double rn = (r0 + z) / r0;
	double fg = (sta0 / sta) * (log(rn) / log(1 + sta0));
	int ffg = floor(fg);

	std::vector<std::pair<int, int>>g_levelStartEndLocation =
	{ { 32,32 },		{ 26, 31 },		{ 23, 25 },		{ 22, 22 },		{ 18, 21 },
	{ 14, 17 },		{ 13, 13 },		{ 10, 12 },		{ 7, 9 } ,		{ 4, 6 } ,		{ 1, 3 } };
	// 1 6 3 1 4 4 1 3 3 3 3 
	int left = 0;
	for (int j = 0; j <= level; ++j)
	{
		int left = 32 - g_levelStartEndLocation[j].second;
		int right = g_levelStartEndLocation[j].first + 32 - g_levelStartEndLocation[j].second - 1;
		int nVal = (((unsigned int)ffg << left) >> right);
		if (j == 3 || j == 6) {
			if (nVal > 1)
				nVal = 1;
		}
		else if (j == 4 || j == 5) {
			if (nVal > 14)
				nVal = 14;
		}
		else if (j == 2 || j >= 7)
		{
			if (nVal > 7)
				nVal = 7;
		}
		else if (j == 1)
		{
			if (nVal > 63)
				nVal = 63;
		}

		if (j == 4 || j == 5) {
			std::string s;
			char nrc = (char)(nVal > 9 ? nVal + 55 : nVal + 48);
			s.resize(1);
			s[0] = nrc;
			hcode.push_back(s);
		}
		else if (j == 1 && nVal < 10) {
			std::string f = "0";
			f.append(std::to_string(nVal));
			hcode.push_back(f.c_str());
		}
		else {
			hcode.push_back(std::to_string(nVal).c_str());
		}
	}

	std::stringbuf buf;
	for (int i = 0; i < code.size(); i++)
	{
		buf.sputn(code[i].c_str(), code[i].size());
		buf.sputn(hcode[i].c_str(), hcode[i].size());
	}

	return buf.str();

}

反算代码后面再写,

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在Verilog中实现北斗C/A码扩频与解扩电路设计需要编写多个模块,下面是一个可能的编码示例: ``` // 扩频码模块 module prn_generator(clk, reset, prn); input clk, reset; output [10:0] prn; // 11位的伪随机码 reg [10:0] shift_reg; always @(posedge clk) begin if (reset) begin shift_reg <= 11'b11111111111; // 初始状态为全1 end else begin shift_reg <= {shift_reg[9:0], shift_reg[10] ^ shift_reg[2] ^ shift_reg[1] ^ shift_reg[0]}; end end assign prn = shift_reg; endmodule // 扩频器模块 module prn_multiplier(clk, reset, prn, signal_in, signal_out); input clk, reset; input [10:0] prn; input [7:0] signal_in; output [7:0] signal_out; reg [7:0] product; always @(posedge clk) begin if (reset) begin product <= 8'b0; end else begin product <= prn[10] ? signal_in : ~signal_in; // 根据扩频码的值进行相应的乘法运算 end end assign signal_out = product; endmodule // 解扩器模块 module prn_despreader(clk, reset, prn, signal_in, signal_out); input clk, reset; input [10:0] prn; input [7:0] signal_in; output [7:0] signal_out; reg [7:0] product; always @(posedge clk) begin if (reset) begin product <= 8'b0; end else begin product <= prn[10] ? signal_in : 8'b0; // 根据扩频码的值进行相应的乘法运算 end end assign signal_out = product; endmodule // 信号发生器模块 module signal_generator(clk, reset, signal_out); input clk, reset; output [7:0] signal_out; reg [31:0] counter; always @(posedge clk) begin if (reset) begin counter <= 32'b0; end else begin counter <= counter + 32'h1; // 以固定的频率递增计数器 end end assign signal_out = {8{counter[22]}}; // 将计数器的第22位复制8次作为输出信号 endmodule // 接收器模块 module signal_receiver(clk, reset, received_signal, prn, signal_out); input clk, reset; input [7:0] received_signal; input [10:0] prn; output [7:0] signal_out; wire [7:0] despread_signal; prn_despreader despreader(clk, reset, prn, received_signal, despread_signal); assign signal_out = despread_signal; endmodule ``` 以上是一个简单的Verilog实现示例,仅供参考。实际应用中需要根据具体的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值