×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# 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

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

Are you trying to do QAM? If not then comm.PhaseFrequencyOffset(Name,Value) won't really help. I can't help much past there as I don't understand what you are actually trying to do. Phase shift a spectrum? This is the function fftseq.

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

(OP)
@GregLocock
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

I don't know how to introduce frequency offsets other than by shifting numbers inside the spectrum, but for phase it is just a case of turning your complex spectrum into mag and arg components, modifying arg, and then recombining them back into a complex spectrum
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?

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

#### Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a partâ€™s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!