## MATLAB live script coding help - Phase/Freq offsets

## MATLAB live script coding help - Phase/Freq offsets

(OP)

Hi I was just wondering if anyone could help me with my code, I am unsure how I would add a PhaseFrequencyOffset command. I read on MathWorks that you can use H = comm.PhaseFrequencyOffset(Name,Value), but I'm not entirely sure how id write this, as every time ive attempted it, it never has any effect on the wave.

What I'm basically looking to do is use both the frequency and phase separately so I can compare the two results afterwards, I want the offsets to only show on the demodulator signal, so id assume there's more code I need to add than just the main command.

Ill leave my current code below but any help would be appreciated as ive been stuck on this for a couple days now.

close all

clear all

clc

%%echo on

t0=.15; % signal duration

ts=1/1500; % sampling interval

fm=3/0.15; % message signal frequency

fc=250; % carrier frequency

a=5; % Modulation index a = amplitude of message/ amplitude of carrier

fs=1/ts; % sampling frequency

t=[0:ts:t0]; % time vector

df=0.3; % desired frequency resolution

% message signal

m=[ones(1,t0/(3*ts)),-2*ones(1,t0/(3*ts)),zeros(1,t0/(3*ts)+1)];

m=cos(2*pi*fm.*t);

c=cos(2*pi*fc.*t); % carrier signal

u=m.*c; % modulated signal

m_n=m/max(abs(m)); % normalized message signal

u=(1+a*m_n).*c; % modulated signal

%received signal

y=u.*c; % mixing

%Local oscillator - change the frequency and phase of c by desired values

[M,m,df1]=fftseq(m,ts,df); % Fourier transform

M=M/fs; % scaling

[U,u,df1]=fftseq(u,ts,df); % Fourier transform

U=U/fs; % scaling

[Y,y,df1]=fftseq(y,ts,df); % Fourier transform

Y=Y/fs; % scaling

f_cutoff=150; % cutoff freq. of the filter

n_cutoff=floor(150/df1); % Design the filter.

f=[0:df1:df1*(length(y)-1)]-fs/2;

H=zeros(size(f));

H(1:n_cutoff)=2*ones(1,n_cutoff);

H(length(f)-n_cutoff+1:length(f))=2*ones(1,n_cutoff);

DEM=H.*Y; % spectrum of the filter output

dem=real(ifft(DEM))*fs; % filter output

% the effect of mixing.

figure(1);

subplot(3,1,1)

plot(m(1:length(t)))

title('Waveform of the Message Signal')

xlabel('Time')

subplot(3,1,2)

plot(u(1:length(t)))

title('Waveform of the Modulated Signal')

xlabel('Time')

subplot(3,1,3)

plot(y(1:length(t)))

title('Waveform of the Mixer Output')

xlabel('Time')

figure(2);

subplot(3,1,1)

plot(f,fftshift(abs(M)))

title('Spectrum of the Message Signal')

xlabel('Frequency')

subplot(3,1,2)

plot(f,fftshift(abs(U)))

title('Spectrum of the Modulated Signal')

xlabel('Frequency')

subplot(3,1,3)

plot(f,fftshift(abs(Y)))

title('Spectrum of the Mixer Output')

xlabel('Frequency')

% the effect of filtering on the mixer output.

figure(3);

subplot(3,1,1)

plot(f,fftshift(abs(Y)))

title('Spectrum of the Mixer Output')

xlabel('Frequency')

subplot(3,1,2)

plot(f,fftshift(abs(H)))

title('Lowpass Filter Characteristics')

xlabel('Frequency')

subplot(3,1,3)

plot(f,fftshift(abs(DEM)))

title('Spectrum of the Demodulator output')

xlabel('Frequency')

% to compare the spectra of the message and the received signal.

figure(4);

subplot(2,1,1)

plot(f,fftshift(abs(M)))

title('Spectrum of the Message Signal')

xlabel('Frequency')

subplot(2,1,2)

plot(f,fftshift(abs(DEM)))

title('Spectrum of the Demodulator Output')

xlabel('Frequency')

% the message and the demodulator output signals.

figure(5);

subplot(2,1,1)

plot(t,m(1:length(t)))

title('The Message Signal')

xlabel('Time')

subplot(2,1,2)

plot(t,dem(1:length(t)))

title('The Demodulator Output')

xlabel('Time')][/code]

What I'm basically looking to do is use both the frequency and phase separately so I can compare the two results afterwards, I want the offsets to only show on the demodulator signal, so id assume there's more code I need to add than just the main command.

Ill leave my current code below but any help would be appreciated as ive been stuck on this for a couple days now.

close all

clear all

clc

%%echo on

t0=.15; % signal duration

ts=1/1500; % sampling interval

fm=3/0.15; % message signal frequency

fc=250; % carrier frequency

a=5; % Modulation index a = amplitude of message/ amplitude of carrier

fs=1/ts; % sampling frequency

t=[0:ts:t0]; % time vector

df=0.3; % desired frequency resolution

% message signal

m=[ones(1,t0/(3*ts)),-2*ones(1,t0/(3*ts)),zeros(1,t0/(3*ts)+1)];

m=cos(2*pi*fm.*t);

c=cos(2*pi*fc.*t); % carrier signal

u=m.*c; % modulated signal

m_n=m/max(abs(m)); % normalized message signal

u=(1+a*m_n).*c; % modulated signal

%received signal

y=u.*c; % mixing

%Local oscillator - change the frequency and phase of c by desired values

[M,m,df1]=fftseq(m,ts,df); % Fourier transform

M=M/fs; % scaling

[U,u,df1]=fftseq(u,ts,df); % Fourier transform

U=U/fs; % scaling

[Y,y,df1]=fftseq(y,ts,df); % Fourier transform

Y=Y/fs; % scaling

f_cutoff=150; % cutoff freq. of the filter

n_cutoff=floor(150/df1); % Design the filter.

f=[0:df1:df1*(length(y)-1)]-fs/2;

H=zeros(size(f));

H(1:n_cutoff)=2*ones(1,n_cutoff);

H(length(f)-n_cutoff+1:length(f))=2*ones(1,n_cutoff);

DEM=H.*Y; % spectrum of the filter output

dem=real(ifft(DEM))*fs; % filter output

% the effect of mixing.

figure(1);

subplot(3,1,1)

plot(m(1:length(t)))

title('Waveform of the Message Signal')

xlabel('Time')

subplot(3,1,2)

plot(u(1:length(t)))

title('Waveform of the Modulated Signal')

xlabel('Time')

subplot(3,1,3)

plot(y(1:length(t)))

title('Waveform of the Mixer Output')

xlabel('Time')

figure(2);

subplot(3,1,1)

plot(f,fftshift(abs(M)))

title('Spectrum of the Message Signal')

xlabel('Frequency')

subplot(3,1,2)

plot(f,fftshift(abs(U)))

title('Spectrum of the Modulated Signal')

xlabel('Frequency')

subplot(3,1,3)

plot(f,fftshift(abs(Y)))

title('Spectrum of the Mixer Output')

xlabel('Frequency')

% the effect of filtering on the mixer output.

figure(3);

subplot(3,1,1)

plot(f,fftshift(abs(Y)))

title('Spectrum of the Mixer Output')

xlabel('Frequency')

subplot(3,1,2)

plot(f,fftshift(abs(H)))

title('Lowpass Filter Characteristics')

xlabel('Frequency')

subplot(3,1,3)

plot(f,fftshift(abs(DEM)))

title('Spectrum of the Demodulator output')

xlabel('Frequency')

% to compare the spectra of the message and the received signal.

figure(4);

subplot(2,1,1)

plot(f,fftshift(abs(M)))

title('Spectrum of the Message Signal')

xlabel('Frequency')

subplot(2,1,2)

plot(f,fftshift(abs(DEM)))

title('Spectrum of the Demodulator Output')

xlabel('Frequency')

% the message and the demodulator output signals.

figure(5);

subplot(2,1,1)

plot(t,m(1:length(t)))

title('The Message Signal')

xlabel('Time')

subplot(2,1,2)

plot(t,dem(1:length(t)))

title('The Demodulator Output')

xlabel('Time')][/code]

## RE: MATLAB live script coding help - Phase/Freq offsets

function [M,m,df]=fftseq(m,ts,df)

% [M,m,df]=fftseq(m,ts,df)

% [M,m,df]=fftseq(m,ts)

%FFTSEQ generates M, the FFT of the sequence m.

% The sequence is zero padded to meet the required frequency resolution df.

% ts is the sampling interval. The output df is the final frequency resolution.

% Output m is the zero padded version of input m. M is the FFT.

fs=1/ts;

if nargin == 2

n1=0;

else

n1=fs/df;

end

n2=length(m);

n=2^(max(nextpow2(n1),nextpow2(n2)));

M=fft(m,n);

m=[m,zeros(1,n-n2)];

df=fs/n;

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

## RE: MATLAB live script coding help - Phase/Freq offsets

To try and sum this up, the code is for a synchronous demodulator model and I am trying to introduce frequency and phase deviations soley into the demodulator signal. Im going to be running the modulation depth at 5 and will be going through frequency offsets of 1kHz to 5kHz followed by phase offsets of 0 to 360 degrees. My issue is that I cant figure out how to introduce frequency and phase deviations into my code in order to see the results on the demodulator signal. Hope this helps more than my previous explanation

## RE: MATLAB live script coding help - Phase/Freq offsets

figure

plot(real(M),imag(M),'r')

mag_M=abs(M);

arg_M=arg(M);

%code to modify arg_M goes here 45 degree shift

arg_M_mod=arg_M+pi/4;

% that's enough of that

M_mod=mag_M.*(cos(arg_M_mod)+i*sin(arg_M_mod));

hold on

plot(real(M_mod),imag(M_mod),'k')

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

## RE: MATLAB live script coding help - Phase/Freq offsets

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?