快排,二分查找

// search_Bin.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

int binSearch(int a[6], int x, int len );

int posFun(int * a, int low, int heigh);


int binSearch(int a[6], int x, int len ){

	int low = 0;
	int heigh = len - 1;
	int mid;

	while (low <= heigh)
	{
		mid = (low + heigh)/2; 
		if (a[mid] < x)
		{
			low = mid + 1;
		}else{
			if(a[mid] > x){
				heigh = mid - 1;
			}else{
				return mid;
			}
		}
	}

	return -100;


}

void quickSort(int a[], int low, int heigh){

	int positacion = 0;
	positacion = posFun(a, low, heigh);

	if (low < heigh)
	{
		quickSort(a, low, positacion -1);
		quickSort(a, positacion + 1, heigh);
	}
}

int posFun(int * a, int low, int heigh){

	int var = a[low];

	//while循环退出的条件是 low == heigh 这点要注意
	while (low < heigh)
	{

		while (low < heigh && (a[heigh] > var ))
			--heigh;
		a[low] = a[heigh];

		while (low < heigh && a[low] < var)
			++low;
		a[heigh] = a[low];

	}

	a[low] = var;

	return low;

}

int _tmain(int argc, _TCHAR* argv[])
{
	int a[6] = {1, 2, 3, 4, 5, 6};

	int resultVal;

	resultVal = binSearch(a, 4, 6);

	printf("the val is %d", resultVal);

	int b[6] = {3, 1, 4, 8, 9};

	quickSort(b, 0, 5);

	
	for (int i = 0; i < 5; i++)
	{
		printf("the val is %d", b[i]);
	}

	return 0;
}

void BubbleSort(int x[],int n)
{
	int i,j;
	bool Exchange;  //记录交换标志
	for(int i=1;i < n - 1; i++)    //最多做n-1趟排序
	{
		Exchange = false;
		for(j=n-1;j>=i;--j)
		{
			if(x[j]>x[j+1])
			{
				x[0] = x[j];
				x[j] = x[j+1];
				x[j+1] = x[0];
				Exchange = true;   //发生了交换,设置标志为真.
			}
		}
		if (!Exchange )      //为发生替换,提前终止算法
			return;

	}
}

import numpy as np import matplotlib.pyplot as plt from scipy.signal import find_peaks, get_window from scipy.ndimage import convolve1d def awgn(signal, snr, method='measured'): """ 添加加性高斯白噪声(AWGN) :param signal: 输入信号(复数或实数) :param snr: 目标信噪比(dB) :param method: 功率计算方式('measured'测量输入功率/'fixed'固定功率=1) :return: 加噪后的信号 """ if method == 'measured': sig_power = np.mean(np.abs(signal) ** 2) else: sig_power = 1.0 # 计算噪声功率 noise_power = sig_power / (10 ** (snr / 10)) # 生成复数噪声 if np.iscomplexobj(signal): noise = np.sqrt(noise_power / 2) * (np.random.randn(*signal.shape) + 1j * np.random.randn(*signal.shape)) else: noise = np.sqrt(noise_power) * np.random.randn(*signal.shape) return signal + noise d_lambda = 0.5 # 阵元间距与波长比 rx_num = 8 # 接收天线阵元数 n = 1000 # 采样快拍数 signum = 3 # 信源数目 theta0 = [-30, 0, 20] # 真实来波角度 snr = 10 # 信噪比 def mvdr(): s = np.random.randn(signum, n) + 1j * np.random.randn(signum, n) # 远场窄带信号 a = np.exp(1j * 2 * np.pi * d_lambda * np.sin(np.deg2rad(theta0))[None, :] * np.arange(rx_num)[:, None]) # 导向矢量 x = a @ s # 接收信号 y = awgn(x, snr, 'measured') # 添加噪声的接收信号 r = y @ y.T.conjugate()/n # 接收数据的协方差矩阵 r_ = np.linalg.inv(r) # 协方差矩阵的逆矩阵 thetascan = np.arange(-90, 90 + 0.1, 0.1) # 扫描角度范围 _as = np.exp(1j * 2 * np.pi * d_lambda * np.sin(np.deg2rad(thetascan))[None, :] * np.arange(rx_num)[:, None]) # 多个导向矢量 num = 0 p = np.zeros(len(thetascan), dtype=np.complex_) # 谱峰函数初始化 for i in thetascan: result = _as[:, num][:, np.newaxis].T.conjugate() @ r_ @ _as[:, num][:, np.newaxis] p[num] = 1 / result.item() num += 1 # print(p[0]) # print(p[600]) # print(p[900]) # print(p[1100]) return p, r_, y def draw(p): plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置显示中文字体 plt.rcParams['axes.unicode_minus'] = False # 设置正常显示符号 thetascan = np.arange(-90, 90 + 0.1, 0.1) # 扫描角度范围 rp = np.real(p) # 取实数部分 ip = np.imag(p) # # 绘制散点图 # plt.scatter(thetascan, rp) # # # 添加标题和标签 # plt.title('功率图') # plt.xlabel('角度') # plt.ylabel('功率') # # # 显示图形 # plt.show() # # # 绘制散点图 # plt.scatter(thetascan, ip) # # # 添加标题和标签 # plt.title('查看复数') # plt.xlabel('角度') # plt.ylabel('功率') # # # 显示图形 # plt.show() # 创建一个包含 1 行 2 列的子图布局 fig, axes = plt.subplots(1, 2, figsize=(12, 6)) # 在第一个子图中绘制实部散点图 axes[0].scatter(thetascan, rp) axes[0].set_title('功率图') axes[0].set_xlabel('角度') axes[0].set_ylabel('功率') # 在第二个子图中绘制虚部散点图 axes[1].scatter(thetascan, ip) axes[1].set_title('功率图复数部分(误差?)') axes[1].set_xlabel('角度') axes[1].set_ylabel('功率') # 显示图形 # plt.show() def locate(p): rp = np.real(p) thetascan = np.arange(-90, 90 + 0.1, 0.1) angle = [] post = [] # 高斯平滑滤波 window_size = 23 sigma = 1 window = get_window(('gaussian', sigma), window_size) smoothed_arr = convolve1d(rp, window / window.sum()) # 查找极大值 peaks, _ = find_peaks(smoothed_arr) # 低频率极大值 for i in peaks: if smoothed_arr[i] > 0.1: angle.append(thetascan[i]) post.append(i) return angle, post def denoise(y, r_, angle, i): # W=R^−1A(A^HR^−1A)^−1b a = np.exp(1j * 2 * np.pi * d_lambda * np.sin(np.deg2rad(angle))[None, :] * np.arange(rx_num)[:, None]) b = np.zeros(len(angle))[:, np.newaxis] b[i] = 1 z = a.T.conjugate() @ r_ @ a z_ = np.linalg.inv(z) w = r_ @ a @ z_ @ b s = w.T.conjugate() @ y return s def drawmei(s): # 进行FFT变换 fft_y = np.fft.fft(s) x = np.arange(n) # 频率个数 abs_y = np.abs(fft_y) # 取复数的绝对值,即复数的模(双边频谱) angle_y = np.angle(fft_y) # 取复数的角度 abs_y = abs_y.flatten() angle_y = angle_y.flatten() plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置显示中文字体 plt.rcParams['axes.unicode_minus'] = False # 设置正常显示符号 # 绘制双边振幅谱(未归一化) plt.figure() plt.plot(x, abs_y) plt.title('双边振幅谱(未归一化)') # 绘制双边相位谱(未归一化) plt.figure() plt.plot(x, angle_y) plt.title('双边相位谱(未归一化)') plt.show() # 按装订区域中的绿色按钮以运行脚本。 if __name__ == '__main__': # 输出方位谱 p, r_, y = mvdr() draw(p) # 声源定位 angle, post = locate(p) # 选择目标信号 print(angle) print('请选择目标信号:') # i = input() i = 1 # 计算降噪信号 s = denoise(y, r_, angle, i) # 画图 drawmei(s) 这段代码是对频域还是时域做的mvdr
最新发布
09-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值