# Frequency translation with FFT

I'm new to DSP and I would like to make an SDR receiver. For this I want to shift bins in the frequency domain in order to bring to 0Hz any portion of the base band spectrum (I know that there are also other methods). I got this idea from SDR for the masses page 4 figure 7

I use a stereo 192KHz 24bit audio codec connected to a dual channel audio generator with 90 degrees phase shift between channels. I use the complex FFT/IFFT from Paul Bourke FFT with 8192 samples length and no window.

If I do FFT and then IFFT I can't notice any signal degradation (I tested it with a single tone and with music). However, if I try to rotate to left with 100 the bins in the first half of I and Q array and to right the second half of array then I can hear a cyclic noise. The single tone output in this case has some distortions at both ends (second picture), as opposed to the first picture where I did not shift the bins.

Also if I shift the bins in the other direction the single tone frequency also seems increase, but not so much(see bellow)! What am I doing wrong? Thanks

void rotate_left(SAMPLE_DATA_TYPE *source, SAMPLE_DATA_TYPE *dest, unsigned short
int x, unsigned short int len) {
memcpy(dest, source + x, (len - x) * sizeof(SAMPLE_DATA_TYPE));
memcpy(dest + len - x, source, x * sizeof(SAMPLE_DATA_TYPE));
}

void rotate_right(SAMPLE_DATA_TYPE *source, SAMPLE_DATA_TYPE *dest, unsigned short int x, unsigned short int len) {
memcpy(dest, source + len - x, x * sizeof(SAMPLE_DATA_TYPE));
memcpy(dest + x, source, (len - x) * sizeof(SAMPLE_DATA_TYPE));
}

main() {

................

#define SAMPLES                 8192
#define FFT_SIZE                13
#define SAMPLE_DATA_TYPE        double
shift = 100;

while (1) {

//Get I and Q from interleaved audio stream
..........

FFT(1, FFT_SIZE, I, Q);
//I and Q have the DC bin at first and last position. At the center is the highest frequency bin

rotate_right(I, temp_buffer_I, shift, SAMPLES / 2);
rotate_left(I + SAMPLES / 2, temp_buffer_I + SAMPLES / 2, shift, SAMPLES / 2);
rotate_right(Q, temp_buffer_Q, shift, SAMPLES / 2);
rotate_left(Q + SAMPLES / 2, temp_buffer_Q + SAMPLES / 2, shift, SAMPLES / 2);

memcpy(I, temp_buffer_I, SAMPLES * sizeof(SAMPLE_DATA_TYPE));
memcpy(Q, temp_buffer_Q, SAMPLES * sizeof(SAMPLE_DATA_TYPE));

FFT(0, FFT_SIZE, I, Q);

//Output I and Q
..........

}

short FFT(short int dir,long m,SAMPLE_DATA_TYPE  *x,SAMPLE_DATA_TYPE *y)
{
long  n,i,i1,j,k,i2,l,l1,l2;
SAMPLE_DATA_TYPE c1,c2,tx,ty,t1,t2,u1,u2,z;

/* Calculate the number of points */
n = 1;
for (i=0;i<m;i++)
n *= 2;

/* Do the bit reversal */
i2 = n >> 1;
j = 0;
for (i=0;i<n-1;i++) {
if (i < j) {
tx = x[i];
ty = y[i];
x[i] = x[j];
y[i] = y[j];
x[j] = tx;
y[j] = ty;
}
k = i2;
while (k <= j) {
j -= k;
k >>= 1;
}
j += k;
}

/* Compute the FFT */
c1 = -1.0;
c2 = 0.0;
l2 = 1;
for (l=0;l<m;l++) {
l1 = l2;
l2 <<= 1;
u1 = 1.0;
u2 = 0.0;
for (j=0;j<l1;j++) {
for (i=j;i<n;i+=l2) {
i1 = i + l1;
t1 = u1 * x[i1] - u2 * y[i1];
t2 = u1 * y[i1] + u2 * x[i1];
x[i1] = x[i] - t1;
y[i1] = y[i] - t2;
x[i] += t1;
y[i] += t2;
}
z =  u1 * c1 - u2 * c2;
u2 = u1 * c2 + u2 * c1;
u1 = z;
}
c2 = sqrt((1.0 - c1) / 2.0);
if (dir == 1)
c2 = -c2;
c1 = sqrt((1.0 + c1) / 2.0);
}

/* Scaling for forward transform */
if (dir == 1) {
for (i=0;i<n;i++) {
x[i] /= n;
y[i] /= n;
}
}

return(1);
}

• Hi BlackSun! Can you show your code as well please... – Fat32 Sep 10 '18 at 0:33
• Hi, I add the most relevant parts of the code. Thank you – BlackSun Sep 10 '18 at 8:10
• This is C code! It could be great if you had Matlab / Octave or at least Python versions... Anyway, lets see if people can debug your code :-) – Fat32 Sep 10 '18 at 11:32
• But I need it to work in C. I never use Matlab ... – BlackSun Sep 10 '18 at 12:05
• You better develop your algorithms using Matlab / Octave / Python / Mathematica / Mapple and port them to C / C++ / Java etc for best results and fastest deployment (on the average). – Fat32 Sep 10 '18 at 12:33