specwidget.cpp 18.8 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1 2 3 4
#include "specwidget.h"
#include "ui_specwidget.h"
#include "qtgui/bookmarks.h"
#include <QSettings>
M
manjaro 已提交
5 6 7 8
#include <QDir>
#include <QDateTime>
#include <QFileInfo>
#include <QFileDialog>
丁劲犇's avatar
丁劲犇 已提交
9 10
specWidget::specWidget(QWidget *parent) :
	QWidget(parent),
11
	ui(new Ui::specWidget),
M
manjaro 已提交
12
	m_attenMod(new QStandardItemModel(this)),
13
	m_rxth(new uhd_io_thread(this)),
M
manjaro 已提交
14
	m_saveth(new uhd_io_thread(this)),
15 16 17 18
	m_txth(new uhd_io_thread(this)),
	m_readth(new uhd_io_thread(this)),
	m_bSaveToFile(false),
	watcher(new stdout_watcher(this))
丁劲犇's avatar
丁劲犇 已提交
19 20 21 22
{
	ui->setupUi(this);
	Bookmarks::create();

M
manjaro 已提交
23
	ui->plotter->setFftFill(false);
丁劲犇's avatar
丁劲犇 已提交
24 25 26 27 28 29 30 31
	ui->plotter->setPeakHold(true);
	ui->plotter->setFftPlotColor(QColor(255,255,255));
	ui->plotter->setRunningState(true);
	ui->plotter->setFreqUnits(1000000);//MHz

	//选择带宽
	ui->plotter->setFilterBoxEnabled(true);

M
manjaro 已提交
32

丁劲犇's avatar
丁劲犇 已提交
33 34 35
	ui->plotter->setTooltipsEnabled(true);
	ui->plotter->setPeakDetection(false,128.0);

M
manjaro 已提交
36 37 38
	m_attenMod->appendRow(new QStandardItem("TX/RX"));
	m_attenMod->appendRow(new QStandardItem("RX2"));
	ui->comboBox_atn->setModel(m_attenMod);
丁劲犇's avatar
丁劲犇 已提交
39 40

	//连接
M
manjaro 已提交
41
	//connect (ui->plotter,&CPlotter::newDemodFreq,ui->freqCtrl,&CFreqCtrl::setFrequency);
丁劲犇's avatar
丁劲犇 已提交
42
	connect (ui->plotter,&CPlotter::newDemodFreq,this, &specWidget::slot_newDemodFreq);
M
manjaro 已提交
43
	connect (ui->freqCtrl,&CFreqCtrl::newFrequency,this, &specWidget::setCenterFreq);
44
	connect (watcher,&stdout_watcher::evt_stdata,this,&specWidget::slot_msg,Qt::QueuedConnection);
M
manjaro 已提交
45
	loadSettings();
46
	watcher->start();
丁劲犇's avatar
丁劲犇 已提交
47 48 49 50
}

specWidget::~specWidget()
{
51 52
	stopProgram();
	watcher->stop_and_wait();
丁劲犇's avatar
丁劲犇 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	delete ui;
}

void specWidget::slot_newDemodFreq(qint64 freq, qint64 delta)/* delta is the offset from the center */
{
	qint64 offfreq = freq - ui->plotter->getCenterFreq();
	if (ui->plotter->getSpanFreq()==0)
		return;
	double ratio = offfreq *1.0/ ui->plotter->getSpanFreq() + 0.5;

	int v = m_nFFTSize * ratio + .5;
	if (v<0) v = 0;

	if (v>=m_nFFTSize)
		v = m_nFFTSize - 1;
	m_nCurrentCenter = v;

}

72 73 74 75 76 77 78
void specWidget::slot_msg(QByteArray s, bool bStdErr)
{
	ui->plainTextEdit_msg->appendPlainText(
				(bStdErr?"STDERR>":"STDIO>") +
				QString::fromUtf8(s));
}

丁劲犇's avatar
丁劲犇 已提交
79
void specWidget::setCenterFreq(quint64 f){
80 81 82 83
	if (f<=0)
		return;
	f = set_rx_freq(f);
	ui->plotter->setCenterFreq(f);
M
manjaro 已提交
84 85 86
	if (ui->doubleSpinBox_center->value() * 1e6 !=ui->freqCtrl->getFrequency())
		ui->doubleSpinBox_center->setValue(ui->freqCtrl->getFrequency()/1e6);
	//ui->plotter->setCenterFreq(f);
丁劲犇's avatar
丁劲犇 已提交
87
}
M
manjaro 已提交
88

丁劲犇's avatar
丁劲犇 已提交
89
void specWidget::setSampleRate(double rate){
90 91 92
	rate = set_rx_rate(rate);
	set_tx_rate(rate);
	ui->plotter->setSampleRate(rate);
丁劲犇's avatar
丁劲犇 已提交
93 94 95 96 97
}

void specWidget::on_pushButton_reset_clicked()
{
	saveSettings();
98
	if (m_rxth->isRunning() || m_txth->isRunning())
M
manjaro 已提交
99
	{
100
		stopProgram();
M
manjaro 已提交
101 102 103

		ui->pushButton_reset->setText(tr("Start"));
	}
104
	else if (ui->checkBox_rx->isChecked() || ui->checkBox_play->isChecked())
M
manjaro 已提交
105 106
	{
		loadSettings();
107
		resetProgram();
M
manjaro 已提交
108
	}
丁劲犇's avatar
丁劲犇 已提交
109 110 111 112 113 114
}

void specWidget::loadSettings()
{
	QSettings settings(QCoreApplication::applicationFilePath()+".ini", QSettings::IniFormat);

M
manjaro 已提交
115
	const bool checkBox_iq = true;
丁劲犇's avatar
丁劲犇 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	const double doubleSpinBox_spr = settings.value("spectrum/doubleSpinBox_spr",10).toDouble();
	ui->doubleSpinBox_spr->setValue(doubleSpinBox_spr);
	//Sample Rate
	ui->plotter->setSampleRate(doubleSpinBox_spr*1000000/(checkBox_iq?1:2));

	const double doubleSpinBox_center = settings.value("spectrum/doubleSpinBox_center",1200).toDouble();
	ui->doubleSpinBox_center->setValue(doubleSpinBox_center);
	//中心频率
	ui->plotter->setCenterFreq(doubleSpinBox_center * 1000000);
	//选择频率
	ui->plotter->setDemodCenterFreq(doubleSpinBox_center*1000000);
	ui->plotter->setHiLowCutFrequencies(-1000000,1000000);

	if (checkBox_iq)
	{
		ui->plotter->setSpanFreq(doubleSpinBox_spr*1000000);
		//选择带宽
		ui->plotter->setMaxBandiwdth(doubleSpinBox_spr*1000000);
	}
	else
	{
		ui->plotter->setSpanFreq(doubleSpinBox_spr*1000000/2);
		//选择带宽
		ui->plotter->setMaxBandiwdth(doubleSpinBox_spr*1000000/2);
	}

	const int spinBox_fftUpdate = settings.value("spectrum/spinBox_fftUpdate",20).toInt();
	ui->spinBox_fftUpdate->setValue(spinBox_fftUpdate);

	const int comboBox_fftSize = settings.value("spectrum/comboBox_fftSize",6).toInt();
	ui->comboBox_fftSize->setCurrentIndex(comboBox_fftSize);

	m_nFFTSize = (1<<comboBox_fftSize) * 128;
	if (m_nFFTSize>65536)
		m_nFFTSize = 65536;
	if (m_nFFTSize<128)
		m_nFFTSize = 128;

M
manjaro 已提交
154 155 156 157 158 159 160 161 162
	const int comboBox_atn = settings.value("spectrum/comboBox_atn",0).toInt();
	ui->comboBox_atn->setCurrentIndex(comboBox_atn);

	const int spinBox_chan = settings.value("spectrum/spinBox_chan",0).toInt();
	ui->spinBox_chan->setValue(spinBox_chan);

	const int spinBox_gain = settings.value("spectrum/spinBox_gain",0).toInt();
	ui->spinBox_gain->setValue(spinBox_gain);

163 164 165 166
	const int spinBox_bw = settings.value("spectrum/spinBox_bw",0).toInt();
	ui->spinBox_bw->setValue(spinBox_bw);


M
manjaro 已提交
167 168 169 170 171 172 173 174 175 176 177 178
	const double doubleSpinBox_range_max = settings.value("spectrum/doubleSpinBox_range_max",0).toInt();
	ui->doubleSpinBox_range_max->setValue(doubleSpinBox_range_max);

	const double doubleSpinBox_range_min = settings.value("spectrum/doubleSpinBox_range_min",-150).toInt();
	ui->doubleSpinBox_range_min->setValue(doubleSpinBox_range_min);

	const QString lineEdit_dev = settings.value("spectrum/lineEdit_dev","").toString();
	ui->lineEdit_dev->setText(lineEdit_dev);

	const QString lineEdit_folder = settings.value("spectrum/lineEdit_folder","./").toString();
	ui->lineEdit_folder->setText(lineEdit_folder);

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	const QString lineEdit_folder_play = settings.value("spectrum/lineEdit_folder_play","./").toString();
	ui->lineEdit_folder_play->setText(lineEdit_folder_play);

	const double doubleSpinBox_center_tx = settings.value("tx/doubleSpinBox_center_tx",1200).toDouble();
	ui->doubleSpinBox_center_tx->setValue(doubleSpinBox_center_tx);

	const int spinBox_chan_tx = settings.value("tx/spinBox_chan_tx",0).toInt();
	ui->spinBox_chan_tx->setValue(spinBox_chan_tx);

	const int spinBox_gain_tx = settings.value("tx/spinBox_gain_tx",0).toInt();
	ui->spinBox_gain_tx->setValue(spinBox_gain_tx);

	const int spinBox_bw_tx = settings.value("tx/spinBox_bw_tx",0).toInt();
	ui->spinBox_bw_tx->setValue(spinBox_bw_tx);


丁劲犇's avatar
丁劲犇 已提交
195 196 197 198 199 200 201 202 203 204 205 206
}
void specWidget::saveSettings()
{
	QSettings settings(QCoreApplication::applicationFilePath()+".ini", QSettings::IniFormat);
	const double doubleSpinBox_spr = ui->doubleSpinBox_spr->value();
	settings.setValue("spectrum/doubleSpinBox_spr",doubleSpinBox_spr);
	const double doubleSpinBox_center = ui->doubleSpinBox_center->value();
	settings.setValue("spectrum/doubleSpinBox_center",doubleSpinBox_center);
	const int spinBox_fftUpdate = ui->spinBox_fftUpdate->value();
	settings.setValue("spectrum/spinBox_fftUpdate",spinBox_fftUpdate);
	const int comboBox_fftSize = ui->comboBox_fftSize->currentIndex();
	settings.setValue("spectrum/comboBox_fftSize",comboBox_fftSize);
M
manjaro 已提交
207 208 209 210
	const int comboBox_atn = ui->comboBox_atn->currentIndex();
	settings.setValue("spectrum/comboBox_atn",comboBox_atn);
	const int spinBox_gain = ui->spinBox_gain->value();
	settings.setValue("spectrum/spinBox_gain",spinBox_gain);
211 212
	const int spinBox_bw = ui->spinBox_bw->value();
	settings.setValue("spectrum/spinBox_bw",spinBox_bw);
M
manjaro 已提交
213 214 215 216 217 218 219 220 221 222 223
	const int spinBox_chan = ui->spinBox_chan->value();
	settings.setValue("spectrum/spinBox_chan",spinBox_chan);
	const double doubleSpinBox_range_max = ui->doubleSpinBox_range_max->value();
	settings.setValue("spectrum/doubleSpinBox_range_max",doubleSpinBox_range_max);
	const double doubleSpinBox_range_min = ui->doubleSpinBox_range_min->value();
	settings.setValue("spectrum/doubleSpinBox_range_min",doubleSpinBox_range_min);

	const QString lineEdit_dev = ui->lineEdit_dev->text();
	settings.setValue("spectrum/lineEdit_dev",lineEdit_dev);
	const QString lineEdit_folder = ui->lineEdit_folder->text();
	settings.setValue("spectrum/lineEdit_folder",lineEdit_folder);
224 225
	const QString lineEdit_folder_play = ui->lineEdit_folder_play->text();
	settings.setValue("spectrum/lineEdit_folder_play",lineEdit_folder_play);
M
manjaro 已提交
226 227


228 229 230 231 232 233 234 235 236 237 238 239
	const double doubleSpinBox_center_tx = ui->doubleSpinBox_center_tx->value();
	settings.setValue("tx/doubleSpinBox_center_tx",doubleSpinBox_center_tx);

	const int spinBox_chan_tx = ui->spinBox_chan_tx->value();
	settings.setValue("tx/spinBox_chan_tx",spinBox_chan_tx);

	const int spinBox_gain_tx = ui->spinBox_gain_tx->value();
	settings.setValue("tx/spinBox_gain_tx",spinBox_gain_tx);

	const int spinBox_bw_tx = ui->spinBox_bw_tx->value();
	settings.setValue("tx/spinBox_bw_tx",spinBox_bw_tx);

M
manjaro 已提交
240

丁劲犇's avatar
丁劲犇 已提交
241
}
242 243

void specWidget::resetProgram()
丁劲犇's avatar
丁劲犇 已提交
244
{
M
manjaro 已提交
245
	ui->pushButton_reset->setText(tr("Start"));
246
	stopProgram();
D
dev@dev.com 已提交
247
	setDevArgs(ui->lineEdit_dev->text().toStdString());
248 249 250 251 252 253 254 255 256 257
	int pg = ui->toolBox_tool->currentIndex();
	ui->toolBox_tool->setCurrentIndex(2);
	initFFTW();
	m_rx_channel = ui->spinBox_chan->value();
	if (!usrp)
		open_device();
	if (!usrp)
		return;
	int updateIntelv = 1000.0 /ui->spinBox_fftUpdate->value() + .5;
	//开始
M
manjaro 已提交
258
	setSampleRate(ui->plotter->getSampleRate());
259 260 261 262 263 264 265 266 267 268 269 270
	initRx();
	initTx();
	m_nTimerID = startTimer(updateIntelv);
	ui->pushButton_reset->setText(tr("Stop"));
	ui->toolBox_tool->setCurrentIndex(pg);
	ui->doubleSpinBox_spr->setEnabled(false);
	ui->spinBox_chan->setEnabled(false);
	ui->spinBox_chan_tx->setEnabled(false);
}

void specWidget::stopProgram()
{
M
manjaro 已提交
271
	if (m_nTimerID>=0)
272
	{
M
manjaro 已提交
273
		killTimer(m_nTimerID);
274 275 276 277 278
		m_nTimerID = -1;
	}
	freeRx();
	freeTx();
	freeFFTW();
M
manjaro 已提交
279 280 281
	if (usrp)
		close_device();

282 283 284 285
	ui->doubleSpinBox_spr->setEnabled(true);
	ui->spinBox_chan->setEnabled(true);
	ui->spinBox_chan_tx->setEnabled(true);
}
丁劲犇's avatar
丁劲犇 已提交
286

M
manjaro 已提交
287

288 289 290 291 292
void specWidget::initRx()
{
	long long bw = ui->spinBox_bw->value()*1000;
	bw = set_rx_bandwidth(bw);
	ui->spinBox_bw->setValue(bw/1000);
M
manjaro 已提交
293
	setCenterFreq(ui->plotter->getCenterFreq());
M
manjaro 已提交
294 295
	QString atn = ui->comboBox_atn->currentText();
	if (atn.size())
296
		set_rx_atn(atn.toStdString());
M
manjaro 已提交
297 298

	on_spinBox_gain_valueChanged(ui->spinBox_gain->value());
299 300
	m_rxth->setRunner([&](void){
		run_RxIO();
M
manjaro 已提交
301
	});
M
manjaro 已提交
302

303 304
	if (!ui->checkBox_rx->isChecked())
		return;
M
manjaro 已提交
305 306
	//保存文件
	m_strFolder = ui->lineEdit_folder->text();
307
	m_rx_points = 0;
M
manjaro 已提交
308
	save_count = 0;
309

M
manjaro 已提交
310 311 312 313 314
	m_bSaveToFile = ui->checkBox_save->isChecked();
	double dfreq = ui->doubleSpinBox_center->value();
	double spr = ui->doubleSpinBox_spr->value();
	m_saveth->setRunner([&,dfreq,spr](void){
		QFile fOut;
315
		const size_t batchsave = 65536;
M
manjaro 已提交
316 317
		while (!stop_signal_called)
		{
318
			if (save_count + m_rxblock_size < m_rx_points && save_count + batchsave < m_rx_points)
M
manjaro 已提交
319
			{
320 321 322
				size_t stasp = (save_count)  % m_rx_bufsz;
				size_t endsp = (save_count +batchsave)  % m_rx_bufsz;
				save_count += batchsave;
M
manjaro 已提交
323 324 325 326 327
				if (!m_bSaveToFile)
				{
					QThread::msleep(10);
					continue;
				}
328 329 330
				if (endsp < batchsave)
					memcpy(&m_data_iq_rx[m_rx_bufsz], m_data_iq_rx, endsp * 2 * sizeof(short) );

M
manjaro 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
				if (fOut.isOpen())
				{
					if (fOut.size()>=1024*1024*1024)
						fOut.close();
				}
				if (!fOut.isOpen())
				{
					QDir dir;
					dir.mkpath(m_strFolder);
					QDateTime dtm = QDateTime::currentDateTime();
					QString sdtm = dtm.toString("yyyy_MM_dd_HH_mm_ss.zzz");
					QString fm = m_strFolder + "/";
					fm += QString("%1MHz_%2Sps_%3.iq.sc16Intel.pcm").arg(dfreq,0,'f',6)
							.arg(spr,0,'f',6).arg(sdtm);
					fOut.setFileName(fm);
					if (!fOut.open(QIODevice::WriteOnly))
						m_bSaveToFile = false;
				}
				if (!m_bSaveToFile)
					continue;
351
				fOut.write((char *)m_data_iq_rx[stasp],batchsave * sizeof(short)*2);
M
manjaro 已提交
352 353 354 355 356 357 358
			}
			else
				QThread::msleep(10);
		}
		if (fOut.isOpen())
			fOut.close();
	});
D
dev@dev.com 已提交
359
	m_rxth->start(QThread::TimeCriticalPriority);
M
manjaro 已提交
360
	m_saveth->start();
丁劲犇's avatar
丁劲犇 已提交
361
}
M
manjaro 已提交
362

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

void specWidget::freeRx()
{
	if (m_rxth->isRunning()||m_saveth->isRunning())
	{
		stop_signal_called = true;
		m_rxth->wait();
		m_saveth->wait();
	}
}


void specWidget::initTx()
{
	long long bw = ui->spinBox_bw_tx->value()*1000;
	bw = set_tx_bandwidth(bw);
	ui->spinBox_bw_tx->setValue(bw/1000);
	set_tx_freq(ui->doubleSpinBox_center_tx->value()*1e6);
	set_tx_gain(ui->spinBox_gain_tx->value());
	m_txth->setRunner([&](void){
		run_TxIO();
	});

386
	m_dataproduce_ok = false;
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	read_count = m_rx_bufsz/2;
	m_tx_points = 0;
	//读取文件
	m_strFolderPlay = ui->lineEdit_folder_play->text();

	QDir cdir(m_strFolderPlay);
	QStringList lst_filter;
	lst_filter << "*.pcm";
	QFileInfoList lstFiles = cdir.entryInfoList(lst_filter,QDir::Files|QDir::Readable,QDir::Name);

	if (!ui->checkBox_play->isChecked() || lstFiles.empty())
		return;

	m_readth->setRunner([&,lstFiles](void){
		QVector<QString> vec_fms;
		foreach (QFileInfo in, lstFiles)
		{
			vec_fms<<in.absoluteFilePath();
		}
		const int szFiles = vec_fms.size();
		int currfile = 0;

		QFile fIn;
		const size_t batchread = 65536;
		while (!stop_signal_called)
		{
			if (m_tx_points + m_tx_bufsz/2 >= read_count )
			{
				if (!fIn.isOpen())
				{
					fIn.setFileName(vec_fms[currfile % szFiles]);
					if (false==fIn.open(QIODevice::ReadOnly))
					{
						fprintf (stderr,"File Can not be opened.");
						stop_signal_called = true;
						continue;
					}
				}
				const size_t cur_begin = read_count % m_tx_bufsz;

				char * addr = (char *)&m_data_iq_tx[cur_begin][0];
				int real_size = fIn.read(addr, batchread * sizeof(short) * 2)/(sizeof(short) * 2);
				read_count += real_size;

				const size_t new_begin = read_count % m_tx_bufsz;

				if (new_begin < batchread)
					memcpy(m_data_iq_tx, &m_data_iq_tx[m_tx_bufsz],sizeof(short)*2*new_begin);

				if (real_size < batchread )
				{
					fIn.close();
					++currfile;
				}
			}
			else
443 444 445
			{
				if (!m_dataproduce_ok)
					m_dataproduce_ok = true;
446
				QThread::msleep(10);
447
			}
448 449 450 451 452 453
		}
		if (fIn.isOpen())
			fIn.close();
	});

	m_readth->start();
454 455
	while (!m_dataproduce_ok && ! stop_signal_called)
		QThread::msleep(10);
456
	m_txth->start(QThread::TimeCriticalPriority);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
}


void specWidget::freeTx()
{
	if (m_txth->isRunning() || m_readth->isRunning())
	{
		stop_signal_called = true;
		m_txth->wait();
		m_readth->wait();
	}
}
void specWidget::initFFTW()
{
	m_pFFTIn = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * m_nFFTSize);
	m_pFFTOut = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * m_nFFTSize);
	m_pFFTPlan = fftw_plan_dft_1d(m_nFFTSize,m_pFFTIn,m_pFFTOut,FFTW_FORWARD,FFTW_ESTIMATE);
	for (int i=0;i<m_nFFTSize;++i)
	{
		m_dHammingWnd.push_back(0.54 - 0.46 * cos (2*3.1415927 * i / (m_nFFTSize-1)));
		m_dFFTAmp.push_back(0);
	}

}

void specWidget::freeFFTW()
{
	if (m_pFFTPlan)
	{
		fftw_destroy_plan(m_pFFTPlan);
		m_pFFTPlan = nullptr;
	}
	if (m_pFFTIn)
	{
		fftw_free(m_pFFTIn);
		m_pFFTIn = nullptr;
	}
	if (m_pFFTOut)
	{
		fftw_free(m_pFFTOut);
		m_pFFTOut = nullptr;
	}
	m_dHammingWnd.clear();
	m_dFFTAmp.clear();
}
丁劲犇's avatar
丁劲犇 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
void specWidget::appendWavComplex(const double (* pWav)[2], const int count, double voltage_ref)
{
	if (!m_pFFTIn || !m_pFFTOut || !m_pFFTPlan || m_dHammingWnd.size()!=m_nFFTSize
			|| m_dFFTAmp.size()!=m_nFFTSize)
		return;
	memset(m_pFFTIn,0,sizeof(fftw_complex)*m_nFFTSize);
	const int nFFTCp = m_nFFTSize<count?m_nFFTSize:count;
	for (long long i=0;i<nFFTCp ;++i)
	{
		long long nHammingIdx = i * m_nFFTSize / nFFTCp;
		m_pFFTIn[i][0] = pWav[i][0] * m_dHammingWnd[nHammingIdx];
		m_pFFTIn[i][1] = pWav[i][1] * m_dHammingWnd[nHammingIdx];
	}
	fftw_execute(m_pFFTPlan);
	const double minus = 10 *log(m_nFFTSize * voltage_ref * m_nFFTSize)/log(10);

	for (long i = 0; i< m_nFFTSize ;++i)
	{
		m_dFFTAmp[(i+m_nFFTSize/2) % m_nFFTSize] = 5 * log (m_pFFTOut[i][0] * m_pFFTOut[i][0] + m_pFFTOut[i][1] * m_pFFTOut[i][1])/log(10) - minus + m_dManuAdd;
	}

M
manjaro 已提交
523
	ui->plotter->setNewFftData(m_dFFTAmp.data(),m_nFFTSize);
丁劲犇's avatar
丁劲犇 已提交
524 525 526 527 528
}


void specWidget::timerEvent(QTimerEvent * e)
{
529
	static std::shared_ptr<double> ppbuf(new double [65536*16]);
M
manjaro 已提交
530
	static double (*pBufIQ)[2] = (double (*)[2])ppbuf.get();
丁劲犇's avatar
丁劲犇 已提交
531
	//int fft
M
manjaro 已提交
532
	if (e->timerId()==m_nTimerID )
丁劲犇's avatar
丁劲犇 已提交
533
	{
534
		if (ui->checkBox_rx->isChecked()==true)
丁劲犇's avatar
丁劲犇 已提交
535
		{
536
			if (!m_bSaveToFile)
丁劲犇's avatar
丁劲犇 已提交
537
			{
538 539 540
				double ref1v = 16384;
				size_t curr_red =(m_rx_points + m_rx_bufsz - m_nFFTSize) % m_rx_bufsz ;
				if (curr_red + m_nFFTSize <= m_rx_bufsz )
M
manjaro 已提交
541
				{
542 543 544 545 546 547 548 549
					int i = 0;
					while (i < m_nFFTSize)
					{
						pBufIQ[i][0] = m_data_iq_rx[i + curr_red][0];
						pBufIQ[i][1] = m_data_iq_rx[i + curr_red][1];
						++i;
					}
					appendWavComplex(pBufIQ,m_nFFTSize,ref1v);
M
manjaro 已提交
550 551

				}
552 553 554 555 556
				if (m_nCurrentCenter>=0 && m_nCurrentCenter < m_nFFTSize)
				{
					ui->sMeter->setLevel(m_dFFTAmp[m_nCurrentCenter]);
				}
				ui->lcdNumber_read->display(int(m_rx_points /1024 / 1024 ));
M
manjaro 已提交
557
			}
558
			else
M
manjaro 已提交
559
			{
560 561 562 563 564 565
				int he = m_rx_points - save_count;

				ui->lcdNumber_saveBehind->display((double)he);

				if (he >= m_rx_bufsz)
					m_bSaveToFile = false;
丁劲犇's avatar
丁劲犇 已提交
566 567
			}
		}
568 569

		if (ui->checkBox_play->isChecked())
丁劲犇's avatar
丁劲犇 已提交
570
		{
571
			size_t he = read_count - m_tx_points;
M
manjaro 已提交
572

573
			ui->lcdNumber_tx->display((double)he/1024.0/1024.0);
M
manjaro 已提交
574

575 576
			if (he <= m_tx_bufsz/16)
				stop_signal_called = true;
丁劲犇's avatar
丁劲犇 已提交
577
		}
M
manjaro 已提交
578

579

M
manjaro 已提交
580 581 582
		static int ccct = 0;
		if (++ccct % 10==0)
		{
583
			if (!m_rxth->isRunning() && !m_txth->isRunning())
M
manjaro 已提交
584 585 586
				ui->pushButton_reset->setText(tr("Start"));
			else
				ui->pushButton_reset->setText(tr("Stop"));
丁劲犇's avatar
丁劲犇 已提交
587

M
manjaro 已提交
588
		}
丁劲犇's avatar
丁劲犇 已提交
589 590 591 592 593 594 595 596 597 598 599 600
	}
}


void specWidget::on_comboBox_fftSize_activated(int index)
{
	if (index)
		m_nFFTSize = 128 * (1<<index);
	if (m_nFFTSize>65536)
		m_nFFTSize = 65536;
	if (m_nFFTSize<128)
		m_nFFTSize = 128;
601
	if (usrp)
M
manjaro 已提交
602 603 604 605
	{
		freeFFTW();
		initFFTW();
	}
丁劲犇's avatar
丁劲犇 已提交
606 607
	saveSettings();
}
M
manjaro 已提交
608 609 610 611 612 613 614 615 616 617 618 619

void specWidget::on_doubleSpinBox_center_valueChanged(double arg1)
{
	if (arg1 * 1e6 != ui->freqCtrl->getFrequency())
	{
		ui->freqCtrl->setFrequency(arg1 * 1e6);
	}
}

void specWidget::on_spinBox_gain_valueChanged(int arg1)
{
	if (usrp)
620 621
		set_rx_gain(arg1);
	//usrp->set_rx_agc(true,m_rx_channel);
M
manjaro 已提交
622 623 624 625 626 627 628 629 630 631 632 633

}

void specWidget::on_doubleSpinBox_range_max_valueChanged(double arg1)
{
	ui->plotter->setWaterfallRange(ui->doubleSpinBox_range_min->value(),ui->doubleSpinBox_range_max->value());
}

void specWidget::on_doubleSpinBox_range_min_valueChanged(double arg1)
{
	ui->plotter->setWaterfallRange(ui->doubleSpinBox_range_min->value(),ui->doubleSpinBox_range_max->value());
}
M
manjaro 已提交
634 635 636 637 638 639

void specWidget::on_comboBox_atn_currentIndexChanged(const QString &arg1)
{
	if (arg1.size())
	{
		if (usrp)
640
			set_rx_atn(arg1.toStdString());
M
manjaro 已提交
641 642 643 644 645
	}
}

void specWidget::on_spinBox_chan_valueChanged(int arg1)
{
646
	m_rx_channel = arg1;
M
manjaro 已提交
647
}
M
manjaro 已提交
648 649 650 651 652 653 654 655 656 657 658 659

void specWidget::on_toolButton_br_clicked()
{
	QString str = QFileDialog::getExistingDirectory(this,tr("Output to"),ui->lineEdit_folder->text());
	if (str.length())
		ui->lineEdit_folder->setText(str);
}

void specWidget::on_checkBox_save_stateChanged(int arg1)
{
	m_bSaveToFile = arg1==0?false:true;
}
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

void specWidget::on_spinBox_bw_valueChanged(int arg1)
{
	double bw = arg1 * 1000;
	if (bw > ui->plotter->getSampleRate())
		bw = ui->plotter->getSampleRate();
	if (bw <0)
		bw = 0;
	bw = set_rx_bandwidth(bw);

}

void specWidget::on_toolButton_br_play_clicked()
{
	QString str = QFileDialog::getExistingDirectory(this,tr("Input from"),ui->lineEdit_folder_play->text());
	if (str.length())
		ui->lineEdit_folder_play->setText(str);
}

void specWidget::on_checkBox_play_stateChanged(int arg1)
{
	m_bPlayFromFile = arg1==0?false:true;
}

void specWidget::on_doubleSpinBox_center_tx_valueChanged(double arg1)
{
	set_tx_freq(arg1 * 1e6);
}

void specWidget::on_spinBox_chan_tx_valueChanged(int arg1)
{
	m_tx_channel = arg1;

}

void specWidget::on_spinBox_gain_tx_valueChanged(int arg1)
{
	set_tx_gain(arg1);
}

void specWidget::on_spinBox_bw_tx_valueChanged(int arg1)
{
	double bw = arg1 * 1000;
	bw = set_tx_bandwidth(bw);
}