help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: FM modulation of a sqarewave carrier with sinusoidal message in gnu-


From: Allen.Windhorn
Subject: RE: FM modulation of a sqarewave carrier with sinusoidal message in gnu-octave
Date: Thu, 29 May 2014 21:59:41 +0000

Kavya,

From: address@hidden
On Behalf Of Kavya Manohar

> Cross posting my question on stackoverflow:
> http://stackoverflow.com/q/23931723/3687167
>
> I have used the code mentioned there in octave to implement
> frequency modulation with sinusoidal message and square wave
> carrier. But the resultant FM waveform has a frequency which is
> not proportional to the message signal amplitude. 
> See the attachment.
>
> What could be the reason. How could it be resolved?

A very similar question was recently asked.  The problem is
that when you change the frequency, you are going all the way
back to the beginning and changing it starting at time zero,
which causes very large phase shifts at the point you are
calculating.

You need to change the frequency starting at the phase angle
of the previous point, so there aren't any sudden phase shifts.
If you do this by calculating the phase change since the last
point it is easier.

Here is some code that works:

%Freqmod 
% Simulate frequency modulation
%
% Allen Windhorn, 29 May 2014
%
% Initialize:
%
format short g; %Display results to more precision
%
% Define funky modulation function
fmod = @(t) (sin(2*pi*21*t)+sin(2*pi*37*t))/2;
% This has peak values around +/-1
%
% Constants:
Cfreq = 200;            % Carrier frequency in Hz
CAmp = 10.0;            % Carrier amplitude (peak)
ModInd = 100.0;         % Hz of shift for 1.0 value of mod function
time = 0:0.00005:0.2;   % Time points to be plotted
%
close all;
%
% Initialize
phase = 0;              % Initial phase angle of carrier
last = 0;               % Previous time value to determine time step
Yv = [];                % Array to accumulate result
%
% AFAIK, this has to be done in a loop, since each value depends on the
% previous value.  Someone else may be able to find a vectorization.
for t = time
  if (t == 0)           % First point has no previous value
    Yv = [0];           % Initialize output array
  else
    f = Cfreq+ModInd*fmod(t);   % Modulated carrier frequency
    phase = phase+(t-last)*2*pi*f;  % Phase increment from frequency
    last = t;           % Time step, could omit if constant
    phase = mod(phase,2*pi);  % Only needed for very long record
    carr = CAmp*sin(phase);   % Next point of carrier wave
    Yv = [Yv;carr];     % Accumulate values in an array
  end
end
%
% Note: this method will accumulate phase errors over time due to round-
% off error, but it really doesn't matter since the frequency is
% changing constantly and the errors should average out.
%
% Plot modulating function
subplot(2,1,1);
plot(time,fmod(time))
% Plot modulated wave
subplot(2,1,2);
plot(time,Yv);

Regards,
Allen

reply via email to

[Prev in Thread] Current Thread [Next in Thread]