Showing results for

- Intel Community
- FPGAs and Programmable Solutions
- Nios® II Embedded Design Suite (EDS)
- Costas Loop oscillates in ModelSim while locks in MATLAB!

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-31-2015
09:21 AM

1,179 Views

Costas Loop oscillates in ModelSim while locks in MATLAB!

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?
46 Replies

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-31-2015
09:54 AM

19 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?
Highlighted
##

--- 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.

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-31-2015
10:23 AM

19 Views

Highlighted
##

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.

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-31-2015
10:39 AM

19 Views

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-31-2015
10:42 AM

19 Views

How to do so?

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-31-2015
10:55 AM

19 Views

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

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-31-2015
06:15 PM

19 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.
Highlighted
##

--- 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?

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
03:44 PM

19 Views

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
04:11 PM

19 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.
Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
06:53 PM

19 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.
Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
07:07 PM

19 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?
Highlighted
##

--- 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.

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
07:27 PM

19 Views

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
07:32 PM

19 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.
Highlighted
##

--- 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

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
07:50 PM

19 Views

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
07:56 PM

19 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,'.');
Highlighted
##

--- 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?

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
08:11 PM

19 Views

Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
08:22 PM

19 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).
Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
08:34 PM

19 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.
Highlighted
##

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
08:46 PM

19 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?
Highlighted
##

--- 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 :)

Altera_Forum

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-01-2015
08:51 PM

19 Views

For more complete information about compiler optimizations, see our Optimization Notice.