Algorithm Issues in Mobile Communications Experiment 2 Correlation Detection

This time, based on the functions implemented last time, some naming irregularities have been modified and file operations have been made more elegant.

1. Introduction

? 1.Correlation detection technology is an important tool in the field of signal detection, often used to detect useful deterministic signals from noise backgrounds. It uses deterministic signals whose values at different times generally have strong correlation, while interference noise is different because of its strong randomness. The characteristic of poor correlation at all times distinguishes deterministic signals from interfering noise.

  1. Assume that the signal sequence is determined to be x[m], and the actual signal sequence with noise background is y[n], m

  2. Sliding correlation is calculated as:

Alt

2. System design requirements

Overall requirements: Confirm whether there is a suitable cell for access among the 12 candidate cells? If so, please find the cell that is most suitable for the terminal to access.

Request to output the printing information of the intermediate process:

1. Print the intensity and sorting results of each cell (the cell ID is the file name);

2. Print the maximum value of the respective sequence obtained after correlating with the 3 sets of PSS;

3. Print the set intensity threshold and related thresholds;

4. Reflect the branch direction of the process (no suitable cell for access, camp failure/current cell cannot be accessed, search for the next cell/current cell meets access conditions, process ends);

3. Design ideas and plans

3.1 Program flow chart design

3.2 Variable design

#define data_N 12//Number of data groups
#define PSS_N 3//Number of PSS files
#define MAX_data 30000 //data stores a total of 30000 data of the real and imaginary parts Q and I of the signal strength
#define MAX_PSS 4096 //PSS stores 4096 rows of data

struct Cell//Cell structure
{<!-- -->
    char ID[15];//Community name - file name
    double avg_strength;//average cell signal strength
    double strength[MAX_data/2];//cell signal strength
    double max_collection[PSS_N];//Maximum value of correlation sequence
};
struct PSS //Determine the signal sequence structure
{<!-- -->
    char ID[15];
    double strength[MAX_PSS/2];//Determined signal strength
} ;

struct Cell cell[data_N]; //cell structure array
struct PSS pss[PSS_N]; //Determined signal sequence structure array

void ID_init()//Initialize cell name
{<!-- -->
    int i;
    for(i=0;i<data_N;i + + )
    {<!-- -->
        sprintf(cell[i].ID,"data%d.txt",i + 24);//ID amplitude data24~data35
    }
    for(i=0;i<PSS_N;i + + )
    {<!-- -->
        sprintf(pss[i].ID,"PSS%d.txt",i);//ID amplitude PSS0~PSS2
    }
}

3.3 File reading and data processing

Complete reading the data file, calculate the signal strength modulus value and average signal strength and store them in the cell structure; read the PSS file and calculate the signal strength modulus value and store it in the PSS array

void fileop_data()//Read the data file, calculate the signal strength modulus and average signal strength and store them in the cell structure
{<!-- -->
    FILE* fp;
    int i,j;
    double sum = 0;
    double Q,I;//Define double-precision floating point numbers to store the real and imaginary parts of signal strength
    for(i=0;i<data_N;i + + )
    {<!-- -->
        if((fp = fopen(cell[i].ID,"r")) == NULL)
        {<!-- -->
            printf("Error opening file");
        }
        else
        {<!-- -->
            for(j=0;j<MAX_data/2;j + + )//
            {<!-- -->
                fscanf(fp,"%lf %lf", & amp;Q, & amp;I);//Read the file and store the values in Q and I
                sum = sum + sqrt(Q * Q + I * I);
                cell[i].strength[j] = sqrt(Q * Q + I * I);//Find the signal strength modulus and store it in the structure
            }
            if(j == MAX_data/2)//Execute the following code when executing MAX/2 times
            {<!-- -->
                cell[i].avg_strength = sum / (MAX_data/2);//Calculate the average signal strength and store it in the structure
                sum = 0;//clear the sum to zero
                j = 0;//clear the number of times
            }
        }
        fclose(fp);
    }
}
void fileop_pss()//Read the PSS file and calculate the signal strength modulus and store it in the PSS array
{<!-- -->
FILE* fp;
double Q, I;
for (int i=0; i <PSS_N; i + + )
{<!-- -->
if ((fp = fopen(pss[i].ID, "r")) == NULL)
{<!-- -->
printf("Error opening PSS file");
}
for (int j=0; j<MAX_PSS/2; j + + )
{<!-- -->
 fscanf(fp, "%lf %lf", & amp;Q, & amp;I);
pss[i].strength[j] = sqrt(Q * Q + I * I);//Calculate the signal strength modulus and store it in the structure
}
fclose(fp);
}
}

3.4 Bubble sort

Perform bubble sorting on the average signal strength of the cells from large to small.

void sort()//Bubble algorithm sorting
{<!-- -->
struct Cell temp;
for (int i=0; i<=data_N-1; i + + )
{<!-- -->
for (int j=0; j<=data_N-1-i; j + + )
{<!-- -->
if (cell[j].avg_strength < cell[j + 1].avg_strength)
{<!-- -->
temp = cell[j];
cell[j] = cell[j + 1];
cell[j + 1] = temp;
}
}
}
}

3.5 Sliding related detection

According to the formula:

The algorithm that can be designed is as follows:

double slide(struct Cell *cell, struct PSS *pss)//Sliding related detection
{<!-- -->
    int i,j;
    double max=0;//used to place the maximum correlation sequence
    double temp;//used to temporarily store the current sequence
    for(i=0; i<MAX_data/2 - MAX_PSS/2 + 1; i + + )//According to the formula i∈[0,n-m]
    {<!-- -->
        temp = 0;
        for(j=0;j<MAX_PSS/2;j + + )
        {<!-- -->
            temp = temp + pss->strength[j] * cell->strength[i + j];
        }
        if(temp>max)
        {<!-- -->
            max = temp;
        }
        return max;
    }
    return 0;
}

3.6 Determine whether access is possible

Since the sorted structure data is stored in order from strong to weak according to the average strength, it is first determined whether the maximum average strength of the cell signal satisfies the set average strength threshold. If it does not meet the set average strength threshold, it will output “No suitable access cell, camp” Failed”; if it is met, use flag to mark the smallest bit that meets the condition; then judge whether the maximum correlation sequence in the cell that meets the average intensity of the strip meets the set correlation threshold; if it is met, output “The current cell meets the access condition, process End”; if the current cell does not meet the conditions, continue to judge whether the next cell meets the conditions.

void IfConnect()//Determine whether to connect based on the input threshold
{<!-- -->
    int i;
    int flag;//Used to mark cells that meet the average intensity threshold
    double strength_threshold;//Define the average strength threshold
    double correlation_threshold; //Define the maximum intensity threshold after correlation
    printf("\\
Please enter the intensity threshold:\\
");
    scanf("%lf", & amp;strength_threshold);
    //Determine whether the average intensity threshold is met
    if(cell[0].avg_strength<strength_threshold)//The maximum value is also lower than the threshold
    {<!-- -->
        printf("\\
No suitable cell for access, camp failed");
        return;
    }
    printf("\\
The cells that meet the average intensity threshold conditions are:\\
");
    for (i=0; i<data_N; i + + )
{<!-- -->
if(cell[i].avg_strength>=strength_threshold)//If the threshold condition is met, print relevant information
{<!-- -->
printf("ID: %s\b\b\b\b Average signal strength: %lf\\
", cell[i].ID, cell[i].avg_strength);
            flag=i;
}
}
    printf("\\
Please enter the relevant threshold: \\
");
    scanf("%lf", & amp;correlation_threshold);
    //Determine whether the maximum correlation threshold is met
    for(i=0;i<=flag;i + + )
    {<!-- -->
        if(cell[i].max_collection[0]>correlation_threshold & amp; & amp;
            cell[i].max_collection[1]>correlation_threshold & amp; & amp;
            cell[i].max_collection[2]>correlation_threshold)
        printf("ID: %s\b\b\b\b The current cell meets the access conditions and the process ends\\
",cell[i].ID);
        else
        printf("ID: %s\b\b\b\b The current cell cannot be accessed, looking for the next cell\\
",cell[i].ID);
    }
}

3.7 Main function

int main()
{<!-- -->
    int i,j;
    ID_init();//Initialize cell name
    printf("Reading file...\\
");
    fileop_pss();
    fileop_data();
    sort();
    printf("\\
The sorting result is:\\
");
    for(int i=0; i<data_N; i + + )
    {<!-- -->
        printf("ID; The average strength of the %s\b\b\b\b signal is: %lf\\
",cell[i].ID,cell[i].avg_strength);
    }
    printf("\\
PSS and community matching results:\\
");
    for(i=0;i<data_N;i + + )//Find the maximum value of the correlation sequence between the actual signal and the determined signal
    {<!-- -->
        for(j=0;j<PSS_N;j + + )
        {<!-- -->
            cell[i].max_collection[j] = slide( & amp;cell[i], & amp;pss[j]);//Give the maximum value of the correlation sequence after sliding correlation to the cell structure
            printf("ID: %s\b\b\b\b Maximum value of correlation sequence with PSS%d: %lf\\
", cell[i].ID, j, cell[i ].max_collection[j]);
        }
    }
    IfConnect();
}

3.8 Overall code

#include 
#include 
#include 
#define data_N 12//Number of data groups
#define PSS_N 3//Number of PSS files
#define MAX_data 30000 //data stores a total of 30000 data of the real and imaginary parts Q and I of the signal strength
#define MAX_PSS 4096 //PSS stores 4096 rows of data

struct Cell//Cell structure
{
    char ID[15];//Community name - file name
    double avg_strength;//average cell signal strength
    double strength[MAX_data/2];//cell signal strength
    double max_collection[PSS_N];//Maximum value of correlation sequence
};
struct PSS //Determine the signal sequence structure
{
    char ID[15];
    double strength[MAX_PSS/2];//Determined signal strength
} ;

struct Cell cell[data_N]; //cell structure array
struct PSS pss[PSS_N]; //Determined signal sequence structure array

void ID_init()//Initialize cell name
{
    int i;
    for(i=0;i
        sprintf(cell[i].ID,"data%d.txt",i + 24);
    }
    for(i=0;i
        sprintf(pss[i].ID,"PSS%d.txt",i);
    }
}
void fileop_data()//Read the data file, calculate the signal strength modulus and average signal strength and store them in the cell structure
{<!-- -->
    FILE* fp;
    int i,j;
    double sum = 0;
    double Q,I;//Define double-precision floating point numbers to store the real and imaginary parts of signal strength
    for(i=0;i<data_N;i + + )
    {<!-- -->
        if((fp = fopen(cell[i].ID,"r")) == NULL)
        {<!-- -->
            printf("Error opening file");
        }
        else
        {<!-- -->
            for(j=0;j<MAX_data/2;j + + )//
            {<!-- -->
                fscanf(fp,"%lf %lf", & amp;Q, & amp;I);//Read the file and store the values in Q and I
                sum = sum + sqrt(Q * Q + I * I);
                cell[i].strength[j] = sqrt(Q * Q + I * I);//Find the signal strength modulus and store it in the structure
            }
            if(j == MAX_data/2)//Execute the following code when executing MAX/2 times
            {<!-- -->
                cell[i].avg_strength = sum / (MAX_data/2);//Calculate the average signal strength and store it in the structure
                sum = 0;//clear the sum to zero
                j = 0;//clear the number of times
            }
        }
        fclose(fp);
    }
}
void fileop_pss()//Read the PSS file and calculate the signal strength modulus and store it in the PSS array
{<!-- -->
FILE* fp;
double Q, I;
for (int i=0; i <PSS_N; i + + )
{<!-- -->
if ((fp = fopen(pss[i].ID, "r")) == NULL)
{<!-- -->
printf("Error opening PSS file");
}
for (int j=0; j<MAX_PSS/2; j + + )
{<!-- -->
 fscanf(fp, "%lf %lf", & amp;Q, & amp;I);
pss[i].strength[j] = sqrt(Q * Q + I * I);//Calculate the signal strength modulus and store it in the structure
}
fclose(fp);
}
}

void sort()//Bubble algorithm sorting
{<!-- -->
struct Cell temp;
for (int i=0; i<=data_N-1; i + + )
{<!-- -->
for (int j=0; j<=data_N-1-i; j + + )
{<!-- -->
if (cell[j].avg_strength < cell[j + 1].avg_strength)
{<!-- -->
temp = cell[j];
cell[j] = cell[j + 1];
cell[j + 1] = temp;
}
}
}
}
double slide(struct Cell *cell, struct PSS *pss)//Sliding correlation detection
{
    int i,j;
    double max=0;//used to place the maximum correlation sequence
    double temp;//used to temporarily store the current sequence
    for(i=0; i
        temp = 0;
        for(j=0;j
            temp = temp + pss->strength[j] * cell->strength[i + j];
        }
        if(temp>max)
        {
            max = temp;
        }
        return max;
    }
    return 0;
}
void IfConnect()//Determine whether it can be connected based on the input threshold
{
    int i;
    int flag;//Used to mark cells that meet the average intensity threshold
    double strength_threshold;//Define the average strength threshold
    double correlation_threshold; //Define the maximum intensity threshold after correlation
    printf("\\
Please enter the intensity threshold:\\
");
    scanf("%lf", & amp;strength_threshold);
    //Determine whether the average intensity threshold is met
    if(cell[0].avg_strength
        printf("\\
No suitable cell for access, camp failed");
        return;
    }
    printf("\\
The cells that meet the average intensity threshold conditions are:\\
");
    for (i=0; i
if(cell[i].avg_strength>=strength_threshold)//If the threshold condition is met, print relevant information
{
printf("ID: %s\b\b\b\b Average signal strength: %lf\\
", cell[i].ID, cell[i].avg_strength);
            flag=i;
}
}
    printf("\\
Please enter the relevant threshold:\\
");
    scanf("%lf", & amp;correlation_threshold);
    //Determine whether the maximum correlation threshold is met
    for(i=0;i<=flag;i + + )
    {
        if(cell[i].max_collection[0]>correlation_threshold & amp; & amp;
            cell[i].max_collection[1]>correlation_threshold & amp; & amp;
            cell[i].max_collection[2]>correlation_threshold)
        printf("ID: %s\b\b\b\b The current cell meets the access conditions and the process ends\\
",cell[i].ID);
        else
        printf("ID: %s\b\b\b\b The current cell cannot be accessed, looking for the next cell\\
",cell[i].ID);
    }
}
int main()
{
    int i,j;
    ID_init();//Initialize cell name
    printf("Reading file...\\
");
    fileop_pss();
    fileop_data();
    sort();
    printf("\\
The sorting result is:\\
");
    for(int i=0; i
        printf("ID; The average strength of the %s\b\b\b\b signal is: %lf\\
",cell[i].ID,cell[i].avg_strength);
    }
    printf("\\
PSS and community matching results:\\
");
    for(i=0;i
        for(j=0;j
            cell[i].max_collection[j] = slide( & amp;cell[i], & amp;pss[j]);//Assign the maximum correlation value after sliding correlation to the cell structure
            printf("ID: %s\b\b\b\b Maximum value of correlation sequence with PSS%d: %lf\\
", cell[i].ID, j, cell[i ].max_collection[j]);
        }
    }
    IfConnect();
}

4. Debugging

5. Experience

6. Opinions

7. Appendix

operation result



The first one is the most suitable cell for access. You only need to slightly modify the code in the Ifconnect() function to find the optimal solution and exit the program immediately. In order to facilitate intuitive understanding, all the situations are printed out.