How to plot Gauss sums ?

4 Ansichten (letzte 30 Tage)
Mohamed Ahmed
Mohamed Ahmed am 24 Jan. 2022
Kommentiert: Paul am 25 Jan. 2022
I'm trying to plot the Gauss sums according to the equation shown in the image s(t), but I keep receiving errors.
Can you please show me what am I doing wrong ?
clear all
close all
clc
%%
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1024; % Length of signal
t = 2*(0:L-1)*T; % Time vector
x = 0;
k = 0;
s = 0;
p = primes(L);
% s(t) = cumsum((k/p)(1:length(p)-1)).*exp(1i*k*t);
for k=1:p-1
s(t) = s(t) + (k/p).*exp(1i*k*t);
end
figure
subplot(2,2,1)
plot(t,s)
title('signal')

Akzeptierte Antwort

Paul
Paul am 24 Jan. 2022
Bearbeitet: Paul am 24 Jan. 2022
There are at least two problems with the code. Assuming you want to compute numerical values of s, the code can't reference s in a functional form, like s(t). For numerical objects, the "t" in s(t) is an index, which should be numerical integers or a logical. So for one value of p, you need to compute one value of an array s for each value of t, and then for each value of t, you need that inner loop over k. If you want more than one value of p, you'll need to make s a 2D array (one dimension for t and the other for p). Here is modification to the code for a single value of p
clear all
close all
clc
%%
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1024; % Length of signal
t = 2*(0:L-1)*T; % Time vector
x = 0;
k = 0;
s = 0;
p = primes(L);
p = 13; % odd prime
% s(t) = cumsum((k/p)(1:length(p)-1)).*exp(1i*k*t);
s = 0*t; % initialzize t
for jj = 1:numel(t)
for k=1:p-1
s(jj) = s(jj) + (k/p).*exp(1i*k*t(jj));
end
end
It's likely that at least the inner loop could be vectorized. However, this code has another problem. As stated in the text of the Question, the term (k/p) does not mean "k divided by p." Rather it is the Legendre symbol L(k,p). So you'll need a function that computes L(k,p).
Check out
doc jacobiSymbol
which would be perfect to use if p is an odd prime. But the problem statement isn't limited to only odd primes. However the wikipedia page for Legendre symbol seems to imply that it is only defined for odd primes. OTOH, the Mathworld page doesn't quite say that p is odd prime is assumed in the defintion (it might just be poorly worded).
  6 Kommentare
Mohamed Ahmed
Mohamed Ahmed am 25 Jan. 2022
I'm trying to have a flat spectrum, which should be one of the properties of this signal
Paul
Paul am 25 Jan. 2022
s(t) is a sum of complex exponentials. The Fourier transform of a complex exponential is Dirac delta, so the spectrum of s(t) should be an train of p-1 delta functions, shouldn't it?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by