基于matlab gui的基带传输系统设计单极性归零码,单极性非归零码,双极性归零码,双极性非归零码程序
时间: 2023-12-11 08:04:20 浏览: 115
以下是一个基于MATLAB GUI的基带传输系统设计,可以实现单极性归零码、单极性非归零码、双极性归零码、双极性非归零码等编码方式的模拟传输。
主要思路是在GUI界面中设置编码方式、传输速率、信号长度等参数,然后根据参数生成相应的编码序列,并进行调制、加噪声、解调、解码等过程,最终输出传输后的信号波形和误码率等性能指标。
以下是单极性归零码、单极性非归零码、双极性归零码、双极性非归零码的程序实现:
1. 单极性归零码
```matlab
function singlePolarNRZ_Callback(hObject, eventdata, handles)
% hObject handle to singlePolarNRZ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global codeType;
codeType = 'singlePolarNRZ';
```
```matlab
function [code, t] = singlePolarNRZ(bits, fs, bitrate)
% 单极性归零码
% bits:比特流
% fs:采样率
% bitrate:比特率
T = 1 / bitrate; % 比特周期
t = 0 : 1 / fs : (length(bits) * T) - 1 / fs; % 时间轴
code = zeros(1, length(t)); % 编码序列
for i = 1 : length(bits)
if bits(i) == 0 % 低电平
code((i - 1) * fs * T + 1 : i * fs * T) = -1;
else % 高电平
code((i - 1) * fs * T + 1 : i * fs * T) = 1;
end
end
end
```
2. 单极性非归零码
```matlab
function singlePolarRZ_Callback(hObject, eventdata, handles)
% hObject handle to singlePolarRZ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global codeType;
codeType = 'singlePolarRZ';
```
```matlab
function [code, t] = singlePolarRZ(bits, fs, bitrate)
% 单极性非归零码
% bits:比特流
% fs:采样率
% bitrate:比特率
T = 1 / bitrate; % 比特周期
t = 0 : 1 / fs : (length(bits) * T) - 1 / fs; % 时间轴
code = zeros(1, length(t)); % 编码序列
for i = 1 : length(bits)
if bits(i) == 0 % 低电平
code((i - 1) * fs * T + 1 : i * fs * T / 2) = -1;
code(i * fs * T / 2 + 1 : i * fs * T) = 0;
else % 高电平
code((i - 1) * fs * T + 1 : i * fs * T / 2) = 1;
code(i * fs * T / 2 + 1 : i * fs * T) = 0;
end
end
end
```
3. 双极性归零码
```matlab
function bipolarNRZ_Callback(hObject, eventdata, handles)
% hObject handle to bipolarNRZ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global codeType;
codeType = 'bipolarNRZ';
```
```matlab
function [code, t] = bipolarNRZ(bits, fs, bitrate)
% 双极性归零码
% bits:比特流
% fs:采样率
% bitrate:比特率
T = 1 / bitrate; % 比特周期
t = 0 : 1 / fs : (length(bits) * T) - 1 / fs; % 时间轴
code = zeros(1, length(t)); % 编码序列
lastLevel = 1; % 上一个电平
for i = 1 : length(bits)
if bits(i) == 0 % 低电平
code((i - 1) * fs * T + 1 : i * fs * T) = -lastLevel;
lastLevel = -lastLevel;
else % 高电平
code((i - 1) * fs * T + 1 : i * fs * T) = lastLevel;
end
end
end
```
4. 双极性非归零码
```matlab
function bipolarRZ_Callback(hObject, eventdata, handles)
% hObject handle to bipolarRZ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global codeType;
codeType = 'bipolarRZ';
```
```matlab
function [code, t] = bipolarRZ(bits, fs, bitrate)
% 双极性非归零码
% bits:比特流
% fs:采样率
% bitrate:比特率
T = 1 / bitrate; % 比特周期
t = 0 : 1 / fs : (length(bits) * T) - 1 / fs; % 时间轴
code = zeros(1, length(t)); % 编码序列
lastLevel = 1; % 上一个电平
for i = 1 : length(bits)
if bits(i) == 0 % 低电平
code((i - 1) * fs * T + 1 : i * fs * T / 2) = -lastLevel;
code(i * fs * T / 2 + 1 : i * fs * T) = lastLevel;
else % 高电平
code((i - 1) * fs * T + 1 : i * fs * T / 2) = lastLevel;
code(i * fs * T / 2 + 1 : i * fs * T) = -lastLevel;
lastLevel = -lastLevel;
end
end
end
```
在GUI界面中,可以设置比特率、码型、码长等参数,然后根据参数生成相应的编码序列,并进行调制、加噪声、解调、解码等过程,最终输出传输后的信号波形和误码率等性能指标。具体实现可以参考以下示例代码:
```matlab
function transmitButton_Callback(hObject, eventdata, handles)
% hObject handle to transmitButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global codeType;
global bitrate;
global signalLength;
global SNR;
global Eb;
global fs;
global noiseVar;
global bits;
global code;
global t;
global modulatedSignal;
global receivedSignal;
global demodulatedSignal;
global decodedBits;
% 设置信号参数
bitrate = str2double(get(handles.bitrateEdit, 'String')); % 比特率
signalLength = str2double(get(handles.lengthEdit, 'String')); % 信号长度
SNR = str2double(get(handles.SNREdit, 'String')); % 信噪比
Eb = 1; % 比特能量
fs = 10 * bitrate; % 采样率
noiseVar = Eb / SNR; % 噪声方差
% 生成随机比特流
bits = randi([0, 1], 1, signalLength);
% 根据选择的码型生成编码序列
switch codeType
case 'singlePolarNRZ'
[code, t] = singlePolarNRZ(bits, fs, bitrate);
case 'singlePolarRZ'
[code, t] = singlePolarRZ(bits, fs, bitrate);
case 'bipolarNRZ'
[code, t] = bipolarNRZ(bits, fs, bitrate);
case 'bipolarRZ'
[code, t] = bipolarRZ(bits, fs, bitrate);
end
% 进行ASK调制
modulatedSignal = code .* sqrt(2 * Eb / bitrate) .* cos(2 * pi * bitrate * t);
% 加入
阅读全文