I have been trying to generate sine and cosine sampled graphs by a given sampling frequency.

fc = 4092; % Carrier frequency Hz

fs = 16368; % Sampling freq Hz
ts = 1/fs; % Sampled time

nn = [0 :4091] % Array with numbers from 0 to fc

y1 = exp(i*2*pi*fc*ts*nn); % Exponential equation

cosine = real(y1);
sine = imag(y1);

axis tight;
grid on;

axis tight;
grid on;
  • 3
    $\begingroup$ What is the problem you are facing? If you zoom into the graph you can see the periodic sin and cos wave. The period is just 4 samples that is why your graph looks like fully filled. $\endgroup$ – jithin Apr 11 '20 at 6:51
  • $\begingroup$ The body of the question is not clear. If you want to generate sampled data, then the graphs would be of the sampled data—which may or may not look like sine and cosine, depending on the the relative frequency. When you say, "generate sine and cosine sampled graphs", it seems to imply you want a result that looks like sine and cosine. Perhaps you can restate you intention. $\endgroup$ – Nigel Redmon Apr 11 '20 at 20:55
  • 2
    $\begingroup$ Why are you trying to generate $\cos(\cdot)$ and $\sin(\cdot)$ with exponentials? $\endgroup$ – robert bristow-johnson Apr 11 '20 at 22:51
  • $\begingroup$ Yeah, I had the same question when I first read it, so I assumed he just wanted to prove to himself that a complex exponential has both cosine and sine components. But after he accepted an answer that doesn't show that, now I don't know... $\endgroup$ – Nigel Redmon Apr 11 '20 at 23:46

If you have defined a sampling frequency then you should use it in defining your time grid "nn", otherwise you are just incrementing in digital steps.

The grid "nn" is basically a digital grid rather than representing true sampling based on your defined sampling frequency. Also the range of your time grid should be in mutliples of the time period of sinusoid (it could be anything but if you want to define it in that terms then define it in terms of time period of sinusoid and not the frequencies itslef).I have modified your code below to view 5 cycles of the sinusoid. Hope it helps

fc = 4092; % carrier frequency Hz

fs = 16368; % sampling freq Hz

nn = [0 : (1/fs):5*(1/fc)] % array with numbers from 0 to fc

y1 = exp(i*2*pi*fc*nn); % exponential equation
  • $\begingroup$ Thanks a lot that works. $\endgroup$ – RohitM Apr 11 '20 at 8:06
  • $\begingroup$ The reason I didn't go this route is that it essentially makes fc and fs meaningless as far as frequency. Worse, their relationship determines the number of points in the plot—for your numbers, there are only 21 points (fs/fc * 5 + 1), and the plot looks like a triangle wave. Try this for fc = 100, a very nice sinusoid. Try it for fc = 10000, yuck. $\endgroup$ – Nigel Redmon Apr 11 '20 at 17:19
  • $\begingroup$ @nigel redmon the way you have defined the time grid and the exponential, where is the notion of continuous time frequency, what you have generated is a discrete time complex exponential, he is looking for a continuous time exponential which is represented in the above answer $\endgroup$ – Dsp guy sam Apr 11 '20 at 17:51
  • $\begingroup$ @Dspguysam Sorry, I don't understand that—you are generating discrete points in your code too. The difference is I don't relate it to fc and fs, but I tell him how he can trivially—because he doesn't include a graphic to make clear what he expects to see. Again, your code plots five cycles of a triangle wave, because the number of samples generated depends on the ratio of fs to fc. Run your code above with the semi-colon omitted from the last statement—21 samples: 1.00000 + 0.00000i 0.00000 + 1.00000i -1.00000 + 0.00000i -0.00000 - 1.00000i 1.00000 - 0.00000i... $\endgroup$ – Nigel Redmon Apr 11 '20 at 18:02
  • $\begingroup$ @nigelRedmon ok, let me ask this, in your code, what is the continuous time frequency of the exponential in Hz? $\endgroup$ – Dsp guy sam Apr 11 '20 at 18:25

You have the basic idea, but first you have some formatting issues as posted (which might not be in what you are executing). And it's not clear what you expect to see (one cycle? more?) in the plots.

The formatting issues: First, "ts = 1/fs;" should be split onto another line, othewise it gets lost in the preceding comment. Second, "pifcts" should be "pi * fs *ts". Equivalently, you can simplify by getting rid of ts altogether and dividing by fs.

One conceptual problem is you define nn as "from 0 to fc". It has no relationship with fc, it's simply how many points you want to plot to the screen.

Finally, because you're plotting a frequency fs of one-fourth the sample rate fc, you'll end up plotting only a quarter cycle. You probably want to see at least a full cycle, so you'd need to multiply by another factor as well.

I'll assume you want to start by seeing one cycle to prove you were right about the complex exponential. So, the plot here is simply 0 to 2pi, one cycle. Multiple the 2*pi by fc/fs if you want to scale for frequency, and if you want to show more cycles in the plots multiply by a factor greater than one to do that.

plotPoints = 1000;  % number of points in plot

nn = [0 : plotPoints - 1];

y1 = exp(i*2*pi * nn/plotPoints); % exponential equation from 0 to 2pi

cosine = real(y1); sine = imag(y1);

subplot(1,2,1); plot(cosine); axis tight; grid on;

subplot(1,2,2); plot(sine); axis tight; grid on;

cosine and sine plots


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.