Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12589 Discussions

Costas Loop oscillates in ModelSim while locks in MATLAB!

Altera_Forum
Honored Contributor II
3,564 Views

Hello again, 

I have designed a Costas Loop in MATLAB and it simulates very well... locking and recovering the carrier. 

Here are the results: 

 

http://www.alteraforum.com/forum/attachment.php?attachmentid=10456&stc=1  

 

I ported the design to FPGA in VHDL, and simulated in ModelSim... however, the loop has a kinda oscillating behaviour... take a look: 

 

http://www.alteraforum.com/forum/attachment.php?attachmentid=10457&stc=1  

 

This is the Loop Filter output: 

 

http://www.alteraforum.com/forum/attachment.php?attachmentid=10458&stc=1  

 

Why is it oscillating? Is it the loop filter?
0 Kudos
46 Replies
Altera_Forum
Honored Contributor II
1,461 Views

Is the filter in floating point in matlab? Or fixed point? 

The VHDL will be fixed point - are there any rounding errors or parts where the bit width isnt wide enough?
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

 

--- Quote Start ---  

Is the filter in floating point in matlab? Or fixed point? 

The VHDL will be fixed point - are there any rounding errors or parts where the bit width isnt wide enough? 

--- Quote End ---  

 

 

I tried to use multiples of 2 as far as possible in matlab. This is the block diagram of the loop filter where C1 = 4 and C2 = 1/2^8 

 

https://www.alteraforum.com/forum/attachment.php?attachmentid=10459  

 

In VHDL, I tried every thing regarding rounding errors and bit widths... this is the best result I got or the closest result to matlab results. I widened vectors in almost every where in loop filter.
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

I highly suggest you convert your matlab model to fixed point, so you can compare the two models directly. Matlab natively uses floating point, so there are bound to be differences.

0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

How to do so?

0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

Mathworks provide a fixed point tool box as part of simulink.

0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

oscillation is good sign as convergence of feedback loops takes time (convergence time) before it settles with some jitter in the steady state. It then may lose lock and re-acquire if upset. All these parameters are well known in control design. 

 

Your matlab model may not be that good I assume. Moreover to match delays and latency and bit precision/truncation is no easy job. I suggest you stick with modelsim for now.
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

 

--- Quote Start ---  

oscillation is good sign as convergence of feedback loops takes time (convergence time) before it settles with some jitter in the steady state. It then may lose lock and re-acquire if upset. All these parameters are well known in control design. 

 

Your matlab model may not be that good I assume. Moreover to match delays and latency and bit precision/truncation is no easy job. I suggest you stick with modelsim for now. 

--- Quote End ---  

 

 

Please don't bring me back to Matlab LOL! 

Don't you think that it is taking too much time to settle? or a 300+ microseconds is normal for convergence time? 

I was thinking of group delays that you once warned me of... is this the time to discuss it?
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

Hi Kazem, I had a doubt about the NCO to be the reason.... I changed it into a DDS I wrote. Take a look at the result: 

 

http://www.alteraforum.com/forum/attachment.php?attachmentid=10465&stc=1  

 

It does converge! and phase error is 0! But data is diminished also... Why do think is this? I remember I faced something like that in Matlab but I don't remember how and why.
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

what is your input? is it clean tone or bpsk signal. 

Where is your model that works in matlab?  

 

a single tone should end up as dc i.e. two constants for I and Q if nco frequency is correctly matched to push input signal to zero freq.(baseband). but for bpsk signal I expect to see constellations of +/- on each I and Q or one circle if nco is not locked (still rotating symbols). 

 

The main difference between model and sim is that of resolution, truncations and loop delay (in unit samples) between nco updates. 

 

Groupdelay of filters doesn't make difference but it affects initial convergence time in both cases and equally.
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

My input is a bpsk signal. 

I didn't get the second question. 

 

What do you mean by "but for bpsk signal I expect to see constellations of +/- on each I and Q"? 

 

I was trying several things before I post this thread... and each one yields to different result... however when I open the loop they all give the same falling slope that I get in matlab if I open the loop. Let's try to eliminate options... 

1- Do you recommend using Altera's NCO (First post in the thread) or go on with my DDS (previous post)? 

2- I tried to use RRC after mixers instead of CIC... but CIC gave more closer results... what do you think about it? would it be a major cause to the problem? 

3- I am scaling down the loop filter output that is used to subtract or add with NCO phi_inc. Am I right?
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

 

--- Quote Start ---  

My input is a bpsk signal. 

I didn't get the second question. 

 

What do you mean by "but for bpsk signal I expect to see constellations of +/- on each I and Q"? 

 

I was trying several things before I post this thread... and each one yields to different result... however when I open the loop they all give the same falling slope that I get in matlab if I open the loop. Let's try to eliminate options... 

1- Do you recommend using Altera's NCO (First post in the thread) or go on with my DDS (previous post)? 

2- I tried to use RRC after mixers instead of CIC... but CIC gave more closer results... what do you think about it? would it be a major cause to the problem? 

3- I am scaling down the loop filter output that is used to subtract or add with NCO phi_inc. Am I right? 

--- Quote End ---  

 

 

Altera nco is ok. "my dds" is an nco if it works! there is no difference but possibly altera's one will have higher resolution (for phase). 

To answer other questions follow this test: 

ignore carrier tracking. Send bpsk signal from tx through rx with nco phase in both tx and rx at zero offset and see (after final RRC) that you get constellations by plotting: 

plot(I_signal,'.'); 

 

you can do that in your model or sim by deactivationg the loop i.e. setting eror to zero at addition to nco word. (I don't use close/open as they can be confusing). 

 

you should see +/- constellations on I axis. if it fails don't leave it to carrier tracking.
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

when doing above test in modelsim make sure the phase of tx nco is same as that of rx nco. If not adjust rx nco to match when they get together. Both sine and cosine waves mustmatch in phase at rx mixer(sine will be inverted for negative frequency) 

 

Regarding rrc/cic I don't know about your chain but normally RRC is first in tx, last in Rx.
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

 

--- Quote Start ---  

 

Regarding rrc/cic I don't know about your chain but normally RRC is first in tx, last in Rx. 

--- Quote End ---  

 

 

Brilliant! One confusion vanished. 

 

I am not sure how to plot constellations in Modelsim but this is the output of I_RRC with no phase offset in both tx and rx, both synchronized and loop is deactivated. 

 

http://www.alteraforum.com/forum/attachment.php?attachmentid=10466&stc=1
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

In fact you should get same patterns as your input stream. A high mean +1 and low means -1 (if nco are in phase). So don't worry about constellation diagrams. 

 

In matlab if y is complex then plot(y,'.') will do. 

for bpsk plot(I,Q*0,'.');
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

 

--- Quote Start ---  

In fact you should get same patterns as your input stream. A high mean +1 and low means -1 (if nco are in phase). So don't worry about constellation diagrams. 

 

In matlab if y is complex then plot(y,'.') will do. 

for bpsk plot(I,Q*0,'.'); 

--- Quote End ---  

 

 

I know how to plot them in matlab... in modelsim, as you mentioned I should see same patterns as in tx... and this is what I sent in tx. 

What do we conclude now? Problem in loop?
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

If tx pattern is same as rx pattern (after slicing) and for every sample then it means the chain is working in principle. 

If you plot constellations before slicing it will show how much scatter you have (due to filters) which could degrade the loop. 

 

Next activate the loop and do some prayer. Remember this simple principle: nco step at start is correct for frequency as frequency is locked.  

With phase loop active the nco step must become same as its initial value otherwise you lose frequency lock as well.  

All the phase lock should do is in effect give it a push (a nodge if my spelling is right) to create offset towards correct phase and revert back to initial increment value. So the error adding to nco word must be really scaled gently. I know some people increment the nco word at slow rate and revert back to initial step after cange of phase accumulator value (offset).
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

If you want a tricky solution then avoid loop altogether and do the following: 

apply a known tx symbol pattern at start. Try detect it at Rx. Keep moving phase offset until you get it. That is all. 

In practice this trick wouldn't work or I will have patented it since frequency needs lock as well.
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

This is greatly useful to know although it's simple... I believe you've just vanished another confusion I had regarding Costas Loop in general! 

I have had the question of why does it change the nco word, based on your note the nco word should change for a little of time and come back... that means it should oscillate because I don't expect it to come back instantly! So my design might be correct! 

From another point of view... this is different from matlab simulation... because the loop output in matlab keeps steady after lock on a constant and doesn't come back to the initial value. And this is correct I think because in matlab I am controlling PHASE of the cosine function which happens to be separate from frequency in a way or another, and this is not the case in Modelsim and NCO where there is only one thing (NCO word) that determines freq and phase. I don't know if you got my point regarding the comparison between matlab and modelsim... but am I right?
0 Kudos
Altera_Forum
Honored Contributor II
1,461 Views

 

--- Quote Start ---  

If you want a tricky solution then avoid loop altogether and do the following: 

apply a known tx symbol pattern at start. Try detect it at Rx. Keep moving phase offset until you get it. That is all. 

In practice this trick wouldn't work or I will have patented it since frequency needs lock as well. 

--- Quote End ---  

 

 

Eventually I am going to attach RF Front-End and receive the signal... plus, this is a graduation project as you know and I can't trick professors :D Although they might not know anything but I really want to do it for the sake of self satisfaction :)
0 Kudos
Altera_Forum
Honored Contributor II
1,405 Views

Data aided lock is common practice and you can add slicer result to the loop to check that +/-1 are at equal distance from zero indicating good phase. I believe you focus on modelsim as it has proper loop and delays. If you want to revert to Matlab you better model nco as per rtl exactly so you have control over phase. 

 

I have posted example nco model on DSPrelated.com long time ago but I now know it got bug in negative frequency when phase accum goes backwards. This cn be repaired or use it for positive freq only and if you need to negate it do that after nco e.g. by inverting sine.
0 Kudos
Reply