- 4.示例
如下是一个用来检测发射、接收成功的matlab小例子:
发射正弦波,并将接收到的信号时域图和频域图显示出来。
clear all;
%% usrp必备,用来检测usrp连接成功,并对平台名称什么的赋值
connectedRadios = findsdru;
if strncmp(connectedRadios(1).Status, 'Success', 7)
radioFound = true;
platform = connectedRadios(1).Platform;
switch connectedRadios(1).Platform
case {'B200','B210'}
address = connectedRadios(1).SerialNum;
case {'N200/N210/USRP2','X300','X310'}
address = connectedRadios(1).IPAddress;
end
else
radioFound = false;
address = '192.168.10.2';
platform = 'N200/N210/USRP2';
end
%% 对发射器接收器进行定义赋值
% rfTxFreq = 2.48e9 ;
rfTxFreq = 467.6625e6;
rfRxFreq = 467.6625e6; % RF Receiver Center Frequency (Hz)
% rfRxFreq = 2.48e9 ;
rfRxFreqCorrection = -4e3; % Frequency calibration compensation value (Hz)
rfRxFreqActual = rfRxFreq + rfRxFreqCorrection;
txRadioGain = 5;
rxRadioGain = 15;
rxRadioDecimationFactor = 500;
rxRadioFrameLength = 4000;
radioTx = comm.SDRuTransmitter('Platform', platform, ...
'IPAddress', address, ...
'CenterFrequency', rfTxFreq,...
'Gain', txRadioGain);
radioRx = comm.SDRuReceiver('Platform', platform, ...
'IPAddress', address, ...
'CenterFrequency', rfRxFreq,...
'Gain', rxRadioGain, ...
'DecimationFactor', rxRadioDecimationFactor, ...
'SamplesPerFrame', rxRadioFrameLength, ...
'OutputDataType', 'single');
%% 显示频域范围内的频率强度图显示函数;显示时域信号的波形,本脚本选的是一个正弦波。
hSpectrumAnalyzer = dsp.SpectrumAnalyzer(...
'Name', 'Frequency of the Sine waveform sent out',...
'Title', 'Frequency of the Sine waveform sent out',...
'SampleRate', 200e3, ...
'YLimits', [-150,30],...
'SpectralAverages', 50, ...
'FrequencySpan', 'Start and stop frequencies', ...
'StartFrequency', -100e3, ...9
'StopFrequency', 100e3,...
'Position', figposition([50 30 30 40]));
hTimeScope = dsp.TimeScope(...
'Name', 'Frequency of the Sine waveform sent out',...
'Title', 'Frequency of the Sine waveform sent out',...
'PlotAsMagnitudePhase', false,...
'SampleRate', 200e3, ...
'TimeSpan', 0.001,...
'YLimits', [-0.2,0.2],...
'Position', figposition([50 30 30 40]));
%% 定义信号源
hSineSource = dsp.SineWave (...
'Frequency', 31e3, ...
'Amplitude', 0.8,...
'ComplexOutput', true, ...
'SampleRate', 200e3, ...
'SamplesPerFrame', 4000, ...
'OutputDataType', 'double');
%% 主函数,循环产生视图
if radioFound
% Loop until the example reaches the target stop time.
timeCounter = 0;
while timeCounter < 30
dataTx = step(hSineSource); % Generate audio waveform
% step(hTimeScope,dataTx);
step(radioTx, dataTx);
% Receiver stream processing
% -----------------------------------------------------------------
[dataRx, lenRx] = step(radioRx);
if lenRx > 0
step(hSpectrumAnalyzer, dataRx);
% plotspecsb(dataRx,1/200e3);
step(hTimeScope,dataRx);
% hTimeScope(dataRx);
end
timeCounter=timeCounter+0.001;
end
else
warning(message('sdru:sysobjdemos:MainLoop'))
end
%% 。。。
release(radioTx)
release(radioRx)
release(hSineSource)
displayEndOfDemoMessage(mfilename)