📄 dmod.m
字号:
function [y, t] = dmod(x, Fc, Fd, Fs, method, M, opt2, opt3)
%DMOD Digital modulation.
% Y = DMOD(X, Fc, Fd, Fs, METHOD, OPT1, OPT2, OPT3) modulates signal X
% with carrier frequency Fc (Hz) using the given method in the input
% variable METHOD. The sampling frequency for X is Fd (Hz). The sampling
% frequency for the complex envelope output is Fs (Hz). It is required
% that Fs>Fc. Fs/Fc must be a positive integer. The time interval
% between two successive points in X is 1/Fd. The column number of the
% modulated signal Y is Fs/Fd times as the length of the input signal X.
% The time interval between two successive points in Y is 1/Fs. When Fs
% is a two element vector, the second element specifies the initial
% phase (rad) in the modulation. For baseband simulation or complex
% envelope simulation, please use DMODCE.
%
% METHOD is a string, which can be one of the following:
% 'ask' M-ary Amplitude shift keying modulation.
% 'psk' M-ary Phase shift keying modulation.
% 'qask' M-ary Quadrature amplitude shift-keying modulation.
% 'fsk' M-ary Frequency shift keying modulation.
% 'msk' Minimum shift keying modulation.
% 'sample' Up sample the input signal.
%
% With the switch '/nmap' appended to the string of METHOD variable, the
% function will not process the digital to analog mapping. The input
% signal is assumed having sampling frequency Fs.
%
% DMOD(METHOD, OPT1, OPT2, OPT3) plot constellation layout.
%
% Use DMOD(METHOD) to view the help for a specific method.
%
% See also DDEMOD, DMODCE, DDEMOD, MODMAP. DEMODMAP, AMOD, ADEMOD.
%
% Wes Wang 1/9/95, 10/5/95
% Copyright (c) 1995-96 by The MathWorks, Inc.
% $Revision: 1.1 $ $Date: 1996/04/01 17:56:46 $
%position for optional parameters.
opt_pos = 6;
plot_const = 0;
if nargin < 1
feval('help','dmod')
return;
elseif isstr(x)
method = lower(deblank(x));
if length(method) < 3
error('Invalid method option for dmod.')
end
if nargin == 1
% help lines for individual modulation method.
addition = 'See also DDEMOD, DMODCE, DDEMODCE, MODMAP, DEMODMAP, AMOD, ADEMOD.';
if method(1:3) == 'qas'
callhelp('dmod.hlp',method(1:4),addition);
else
callhelp('dmod.hlp',method(1:3),addition);
end
else
%plot constellation, make a shift.
opt_pos = opt_pos - 3;
M = Fc;
if nargin >= opt_pos
opt2 = Fd;
else
modmap(method, M);
return;
end;
if nargin >= opt_pos+1
opt3 = Fs;
else
modmap(method, M, opt2);
return;
end;
%take care of all plot by using modmap
modmap(method, M, opt2, opt3);
end;
return;
end;
len_x = length(x);
if length(Fs) > 1
ini_phase = Fs(2);
Fs = Fs(1);
else
ini_phase = 0;
end;
if length(Fd) > 1
offset = Fd(2);
Fd = Fd(1);
else
offset = 0;
end;
if (Fs == 0) | (Fd == 0) | isempty(Fd) | isempty(Fs)
FsDFd = 1;
else
FsDFd = Fs / Fd;
if (ceil(FsDFd) ~= FsDFd) | (FsDFd <= 0)
error('Fs / Fd must be a positive integer.')
end;
end;
%turn to the right position.
[r, c] = size(x);
if r * c == 0
y = [];
return;
end;
if r == 1
x = x(:);
len_x = c;
else
len_x = r;
end;
%expand x from Fd to Fs.
if isempty(findstr(method, '/nomap'))
yy = [];
for i = 1 : size(x, 2)
tmp = x(:, ones(1, FsDFd)*i)';
yy = [yy tmp(:)];
end;
x = yy;
clear yy tmp
end;
offset = rem(offset, FsDFd);
if offset < 0
offset = rem(offset + FsDFd, FsDFd);
end;
if (nargin < 3)
disp('Usage: Y=DMOD(X, Fc, Fd, Fs, METHOD, OPT1, OPT2, OPT3) for modulation mapping');
return;
elseif nargin < opt_pos-1
method = 'samp';
end;
% determine M
if nargin < opt_pos
% this will happen only
M = max(max(x)) + 1;
M = 2^(ceil(log(M)/log(2)));
M = max(2, M);
end;
method = lower(method);
if length(method) < 3
method = [method ' '];
end;
if strcmp(method(1:3), 'ask')
if isempty(findstr(method, '/nomap'))
y = (x - (M - 1) / 2 ) * 2 / (M - 1);
else
y = x;
end;
[y, t] = amod(y, Fc, [Fs, ini_phase], 'amdsb-sc');
elseif findstr(method, 'fsk')
if nargin < opt_pos + 1
Tone = 2 * Fd / M;
else
Tone = opt2;
end;
if isempty(findstr(method, '/nomap'))
x = x * Tone;
end;
[len_y, wid_y] = size(x);
t = (0:1/Fs:((len_y-1)/Fs))';
t = t(:, ones(1, wid_y));
x = 2 /Fs * pi * x;
for i = 1 : ceil(len_y/FsDFd)
y((i-1)*FsDFd+1 : min(i*FsDFd, len_y), :) = ...
cos(2 * pi * Fc * t(1:min(FsDFd, len_y-(i-1)*FsDFd), :) ...
+ cumsum([zeros(1, wid_y); x((i-1)*FsDFd+1 : min(i*FsDFd, len_y)-1, :)]) ...
+ ini_phase);
end;
t = (0:1/Fs:((len_y-1)/Fs))';
elseif findstr(method, 'psk')
%PSK is a special case of QASK.
[len_y, wid_y] = size(x);
t = (0:1/Fs:((len_y-1)/Fs))';
if findstr(method, '/nomap')
y = dmod(x, Fc, Fs, [Fs, ini_phase], 'qask/cir/nomap', M);
else
y = dmod(x, Fc, Fs, [Fs, ini_phase], 'qask/cir', M);
end;
elseif findstr(method, 'msk')
%This is a special case of fsk call back to get fsk
M = 2;
Tone = Fd;
if isempty(findstr(method, '/nomap'))
x = x * Tone;
end;
[len_y, wid_y] = size(x);
t = (0:1/Fs:((size(x,1)-1)/Fs))';
t = t(:, ones(1, size(x, 2)));
x = 2 /Fs * pi * x;
for i = 1 : ceil(len_y/FsDFd)
y((i-1)*FsDFd+1 : min(i*FsDFd, len_y), :) = ...
cos(2 * pi * Fc * t(1:min(FsDFd, len_y-(i-1)*FsDFd), :)+...
cumsum([zeros(1,wid_y); x((i-1)*FsDFd+1:min(i*FsDFd,len_y)-1, :)])...
+ ini_phase);
end;
elseif ~isempty(findstr(method, 'qas')) |...
~isempty(findstr(method, 'qam')) |...
~isempty(findstr(method, 'qsk'))
if findstr(method,'nomap')
[y, t] = amod(x, Fc, [Fs, ini_phase], 'qam');
else
if findstr(method, '/ar')
% arbitraryly defined I, Q.
if nargin < opt_pos + 1
error('In correct format for METHOD=''qask/arbitrary''.');
end;
I = M;
Q = opt2;
M = length(I);
% leave to the end for processing
CMPLEX = I + j*Q;
elseif findstr(method, '/ci')
% circle defined NIC, AIC, PIC.
if nargin < opt_pos
error('In correct format for METHOD=''qask/circle''.');
end;
NIC = M;
M = length(NIC);
if nargin < opt_pos+1
AIC = [1 : M];
else
AIC = opt2;
end;
if nargin < opt_pos + 2
PIC = NIC * 0;
else
PIC = opt3;
end;
CMPLEX = apkconst(NIC, AIC, PIC);
M = sum(NIC);
else
%consider as square style.
[I, Q] = qaskenco(M);
CMPLEX = I + j * Q;
end;
y = [];
x = x + 1;
if (min(min(x)) < 1) | (max(max(x)) > M)
error('Element in input X exceeded range.');
end;
if max(max(x)) > length(CMPLEX)
error(['Maximum element in x exceeds limit M = ' num2str(length(CMPLEX))])
elseif min(min(x)) < 1
error('Element in input X cannot be a negative number.')
end;
for i = 1 : size(x, 2)
tmp = CMPLEX(x(:, i));
y = [y tmp(:)];
end;
y = [real(y) imag(y)];
wid_y = size(y, 2);
y = [y(:,1:2:wid_y) y(:,2:2:wid_y)];
[y, t] = amod(y, Fc, [Fs, ini_phase], 'qam');
end;
elseif findstr(method, 'sam')
%This is made possible to convert an input signal from sampling frequency Fd
%to sampling frequency Fs.
[len_y, wid_y] = size(x);
t = (0:1/Fs:((len_y-1)/Fs))';
y = x;
else
%The choice is not a valid one.
disp('You have used an invalid method. The method should be one of the following string:')
disp(' ''ask'' Amplitude shift keying modulation;')
disp(' ''psk'' Phase shift keying modulation;')
disp(' ''qask'' Quadrature amplitude shift-keying modulation, square constellation;')
disp(' ''qask/cir'' Quadrature amplitude shift-keying modulation, circle constellation;')
disp(' ''qask/arb'' Quadrature amplitude shift-keying modulation, user defined constellation;')
disp(' ''fsk'' Frequency shift keying modulation;')
disp(' ''msk'' Minimum shift keying modulation;')
disp(' ''sample'' Convert sample frequency Fd input to sample frequency Fs output.')
end;
%---end of dmod.m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -