Exemplo n.º 1
0
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void SpectrumWidget::updateData() {
	_powerSpectrum.clear();
	_responseCorrectedPowerSpectrum.clear();
	_responsePowerSpectrum.clear();

	if ( (_spec.size() < 2) || (_freqNyquist <= 0) ) {
		_xAxis.setRange(_powerSpectrum.x);
		return;
	}

	// Ignore the first value, this is the offset
	_powerSpectrum.y.resize(_spec.size()-1);
	if ( _phaseSpectrum ) {
		for ( size_t i = 1; i < _spec.size(); ++i )
			_powerSpectrum.y[i-1] = rad2deg(arg(_spec[i]));
	}
	else {
		for ( size_t i = 1; i < _spec.size(); ++i )
			_powerSpectrum.y[i-1] = abs(_spec[i]);
	}

	_powerSpectrum.x.lower = _freqNyquist / _powerSpectrum.count();
	_powerSpectrum.x.upper = _freqNyquist;

	if ( (_powerSpectrum.count() > 1) && _resp ) {
		Math::Restitution::FFT::TransferFunctionPtr tf = _resp->getTransferFunction();
		if ( tf ) {
			_responseCorrectedPowerSpectrum.y.resize(_spec.size()-1);
			_responseCorrectedPowerSpectrum.x = _powerSpectrum.x;

			_responsePowerSpectrum.y.resize(_spec.size()-1);
			_responsePowerSpectrum.x = _powerSpectrum.x;

			double dx = _responseCorrectedPowerSpectrum.x.length() / (_spec.size()-1);
			double px = _responseCorrectedPowerSpectrum.x.lower;

			if ( _phaseSpectrum ) {
				for ( size_t i = 1; i < _spec.size(); ++i, px += dx ) {
					Math::Complex r;
					tf->evaluate(&r, 1, &px);
					_responsePowerSpectrum.y[i-1] = rad2deg(arg(r));
					_responseCorrectedPowerSpectrum.y[i-1] = rad2deg(arg(_spec[i] / r));
				}
			}
			else {
				for ( size_t i = 1; i < _spec.size(); ++i, px += dx ) {
					Math::Complex r;
					tf->evaluate(&r, 1, &px);
					_responsePowerSpectrum.y[i-1] = abs(r);
					_responseCorrectedPowerSpectrum.y[i-1] = abs(_spec[i] / r);
				}
			}
		}
	}

	_xAxis.setRange(_powerSpectrum.x);
}
Exemplo n.º 2
0
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void SpectrumWidget::exportSpectra() {
	if ( (_spec.size() < 1) || (_freqNyquist <= 0) ) {
		QMessageBox::critical(this, tr("Save spectra"), tr("No data"));
		return;
	}

	QString fn = QFileDialog::getSaveFileName(this, tr("Save spectra"), "spec-" + _exportBasename + ".txt");
	if ( fn.isEmpty() ) return;

	ofstream ofs;
	ofs.open(fn.toAscii());
	if ( !ofs.is_open() ) {
		QMessageBox::critical(this, tr("Save spectra"), tr("Failed to open/create %1").arg(fn));
		return;
	}

	ofs << "# This is an ASCII representation of a spectrum. The first column is" << endl
	    << "# the frequency is Hz. The second and third column represent the" << endl
	    << "# complex value (real, imag) at that frequency of the raw spectrum." << endl
	    << "# The amplitude and phase can be found in column four and five." << endl;

	Math::Restitution::FFT::TransferFunctionPtr tf = _resp ? _resp->getTransferFunction() : NULL;
	if ( (_spec.size() > 1) && tf ) {
		ofs
		<< "# The sixth and seventh column represent the complex value of the response" << endl
		<< "# corrected spectrum. Column eight and nine the amplitude and phase" << endl
		<< "# respectively." << endl;
	}
	ofs << "#" << endl
	    << "# Note that phases are output in degrees!" << endl;

	double f = 0;
	double df = _freqNyquist / (_spec.size()-1);

	for ( size_t i = 0; i < _spec.size(); ++i, f += df ) {
		ofs << f << "\t" << _spec[i].real() << "\t" << _spec[i].imag()
		    << "\t" << abs(_spec[i]) << "\t" << deg2rad(arg(_spec[i]));
		if ( tf ) {
			ofs << "\t";
			if ( i ) {
				Math::Complex r;
				tf->evaluate(&r, 1, &f);
				r = _spec[i] / r;
				ofs << r.real() << "\t" << r.imag() << "\t" << abs(r) << "\t" << deg2rad(arg(r));
			}
			else
				ofs << _spec[i].real() << "\t" << _spec[i].imag() << "\t" << abs(_spec[i]) << "\t" << deg2rad(arg(_spec[i]));
		}
		ofs << endl;
	}
}