Summarizing all points around a center point? - c

for the purpose of smoothing an image, I need to summarize all pixels around a given center pixel and then build the average, what's basically my new pixel then.
There are like two problems now:
1) What's a good way to summarize them? and
2) How can I best avoid the corner pixels?
This is what I did so far, but I don't think it's any good.
for(i = 0; i < image1->nx; i++)
{
for(j = 0; j < image1->ny; j++)
{
if(i == 0 || j == 0 || i == image1->nx - 1 || j == image1->ny - 1)
{
image2->image[i][j] = image1->image[i][j];
}
else
{
int average = 0;
average += image1->image[i][j];
average += image1->image[i+1][j];
average += image1->image[i][j+1];
average += image1->image[i+1][j+1];
average += image1->image[i-1][j];
average += image1->image[i-1][j+1];
average += image1->image[i-1][j-1];
average += image1->image[i][j-1];
average += image1->image[i+1][j-1];
average /= 9;
image2->image[i][j] = average;
}
}
}
My struct in C is something like this:
struct pgm_image
{
int nx; // row number
int ny; // cell number
unsigned char image[N1][N2]; // image information
};

This looks right. To shorten it a bit, I guess you could use a for loop.
int min = -1;
int max = 1;
int average = 0;
int amount = 0;
for(int k = min; k <= max; k++){
for(int l = min; l <= max; l++){
amount++;
average += image1->image[i+k][j+l];
}
}
average /= amount;
image2->image[i][j] = average;
It still looks messy, but this way you can alter the smooth "radius" by changing the min and max variables.

Related

I am trying to improve the performance speed of my cross-correlation algorithm. What things can I do to make my C code run faster?

I created a cross-correlation algorithm, and I am trying to maximize its performance by reducing the time it takes for it to run. First of all, I reduced the number of function calls within the "crossCorrelationV2" function. Second, I created several macros at the top of the program for constants. Third, I reduced the number of loops that are inside the "crossCorrelationV2" function. The code that you see is the most recent code that I have.
Are there any other methods I can use to try and reduce the processing time of my code?
Let's assume that I am only focused on the functions "crossCorrelationV2" and "createAnalyzingWave".
I would be glad for any advice, whether in general about programming or pertaining to those two specific functions; I am a beginner programmer. Thanks.
#include <stdio.h>
#include <stdlib.h>
#define ARRAYSIZE 4096
#define PULSESNUMBER 16
#define DATAFREQ 1300
// Print the contents of the array onto the console.
void printArray(double array[], int size){
int k;
for (k = 0; k < size; k++){
printf("%lf ", array[k]);
}
printf("\n");
}
// Creates analyzing square wave. This square wave has unity (1) magnitude.
// The number of high values in each period is determined by high values = (analyzingT/2) / time increment
void createAnalyzingWave(double analyzingFreq, double wave[]){
int highValues = (1 / analyzingFreq) * 0.5 / ((PULSESNUMBER * (1 / DATAFREQ) / ARRAYSIZE));
int counter = 0;
int p;
for(p = 1; p <= ARRAYSIZE; p++){
if ((counter % 2) == 0){
wave[p - 1] = 1;
} else{
wave[p - 1] = 0;
}
if (p % highValues == 0){
counter++;
}
}
}
// Creates data square wave (for testing purposes, for the real implementation actual ADC data will be used). This
// square wave has unity magnitude.
// The number of high values in each period is determined by high values = array size / (2 * number of pulses)
void createDataWave(double wave[]){
int highValues = ARRAYSIZE / (2 * PULSESNUMBER);
int counter = 0;
int p;
for(p = 0; p < ARRAYSIZE; p++){
if ((counter % 2) == 0){
wave[p] = 1;
} else{
wave[p] = 0;
}
if ((p + 1) % highValues == 0){
counter++;
}
}
}
// Finds the average of all the values inside an array
double arrayAverage(double array[], int size){
int i;
double sum = 0;
// Same thing as for(i = 0; i < arraySize; i++)
for(i = size; i--; ){
sum = array[i] + sum;
}
return sum / size;
}
// Cross-Correlation algorithm
double crossCorrelationV2(double dataWave[], double analyzingWave[]){
int bigArraySize = (2 * ARRAYSIZE) - 1;
// Expand analyzing array into array of size 2arraySize-1
int lastArrayIndex = ARRAYSIZE - 1;
int lastBigArrayIndex = 2 * ARRAYSIZE - 2; //bigArraySize - 1; //2 * arraySize - 2;
double bigAnalyzingArray[bigArraySize];
int i;
int b;
// Set first few elements of the array equal to analyzingWave
// Set remainder of big analyzing array to 0
for(i = 0; i < ARRAYSIZE; i++){
bigAnalyzingArray[i] = analyzingWave[i];
bigAnalyzingArray[i + ARRAYSIZE] = 0;
}
double maxCorrelationValue = 0;
double currentCorrelationValue;
// "Beginning" of correlation algorithm proper
for(i = 0; i < bigArraySize; i++){
currentCorrelationValue = 0;
for(b = lastBigArrayIndex; b > 0; b--){
if (b >= lastArrayIndex){
currentCorrelationValue = dataWave[b - lastBigArrayIndex / 2] * bigAnalyzingArray[b] + currentCorrelationValue;
}
bigAnalyzingArray[b] = bigAnalyzingArray[b - 1];
}
bigAnalyzingArray[0] = 0;
if (currentCorrelationValue > maxCorrelationValue){
maxCorrelationValue = currentCorrelationValue;
}
}
return maxCorrelationValue;
}
int main(){
int samplesNumber = 25;
double analyzingFreq = 1300;
double analyzingWave[ARRAYSIZE];
double dataWave[ARRAYSIZE];
createAnalyzingWave(analyzingFreq, analyzingWave);
//createDataWave(arraySize, pulsesNumber, dataWave);
double maximumCorrelationArray[samplesNumber];
int i;
for(i = 0; i < samplesNumber; i++){
createDataWave(dataWave);
maximumCorrelationArray[i] = crossCorrelationV2(dataWave, analyzingWave);
}
printf("Average of the array values: %lf\n", arrayAverage(maximumCorrelationArray, samplesNumber));
return 0;
}
The first point is that you are explicitly shifting the analizingData array, this way you are required twice as much memory and moving the items is about 50% of your time. In a test here using crossCorrelationV2 takes 4.1 seconds, with the implementation crossCorrelationV3 it runs in ~2.0 seconds.
The next thing is that you are spending time multiplying by zero on the padded array, removing that, and also removing the padding, and simplifying the indices we end with crossCorrelationV4 that makes the program to run in ~1.0 second.
// Cross-Correlation algorithm
double crossCorrelationV3(double dataWave[], double analyzingWave[]){
int bigArraySize = (2 * ARRAYSIZE) - 1;
// Expand analyzing array into array of size 2arraySize-1
int lastArrayIndex = ARRAYSIZE - 1;
int lastBigArrayIndex = 2 * ARRAYSIZE - 2; //bigArraySize - 1; //2 * arraySize - 2;
double bigAnalyzingArray[bigArraySize];
int i;
int b;
// Set first few elements of the array equal to analyzingWave
// Set remainder of big analyzing array to 0
for(i = 0; i < ARRAYSIZE; i++){
bigAnalyzingArray[i] = analyzingWave[i];
bigAnalyzingArray[i + ARRAYSIZE] = 0;
}
double maxCorrelationValue = 0;
double currentCorrelationValue;
// "Beginning" of correlation algorithm proper
for(i = 0; i < bigArraySize; i++){
currentCorrelationValue = 0;
// Instead of checking if b >= lastArrayIndex inside the loop I use it as
// a stopping condition.
for(b = lastBigArrayIndex; b >= lastArrayIndex; b--){
// instead of shifting bitAnalizing[b] = bigAnalyzingArray[b-1] every iteration
// I simply use bigAnalizingArray[b-i]
currentCorrelationValue = dataWave[b - lastBigArrayIndex / 2] * bigAnalyzingArray[b - i] + currentCorrelationValue;
}
bigAnalyzingArray[0] = 0;
if (currentCorrelationValue > maxCorrelationValue){
maxCorrelationValue = currentCorrelationValue;
}
}
return maxCorrelationValue;
}
// Cross-Correlation algorithm
double crossCorrelationV4(double dataWave[], double analyzingWave[]){
int bigArraySize = (2 * ARRAYSIZE) - 1;
// Expand analyzing array into array of size 2arraySize-1
int lastArrayIndex = ARRAYSIZE - 1;
int lastBigArrayIndex = 2 * ARRAYSIZE - 2; //bigArraySize - 1; //2 * arraySize - 2;
// I will not allocate the bigAnalizingArray here
// double bigAnalyzingArray[bigArraySize];
int i;
int b;
// I will not copy the analizingWave to bigAnalyzingArray
// for(i = 0; i < ARRAYSIZE; i++){
// bigAnalyzingArray[i] = analyzingWave[i];
// bigAnalyzingArray[i + ARRAYSIZE] = 0;
// }
double maxCorrelationValue = 0;
double currentCorrelationValue;
// Compute the correlation by symmetric paris
// the idea here is to simplify the indices of the inner loops since
// they are computed more times.
for(i = 0; i < lastArrayIndex; i++){
currentCorrelationValue = 0;
for(b = lastArrayIndex - i; b >= 0; b--){
// instead of shifting bitAnalizing[b] = bigAnalyzingArray[b-1] every iteration
// I simply use bigAnalizingArray[b-i]
currentCorrelationValue += dataWave[b] * analyzingWave[b + i];
}
if (currentCorrelationValue > maxCorrelationValue){
maxCorrelationValue = currentCorrelationValue;
}
if(i != 0){
currentCorrelationValue = 0;
// Correlate shifting to the other side
for(b = lastArrayIndex - i; b >= 0; b--){
// instead of shifting bitAnalizing[b] = bigAnalyzingArray[b-1] every iteration
// I simply use bigAnalizingArray[b-i]
currentCorrelationValue += dataWave[b + i] * analyzingWave[b];
}
if (currentCorrelationValue > maxCorrelationValue){
maxCorrelationValue = currentCorrelationValue;
}
}
}
return maxCorrelationValue;
}
If you want more optimization you can unroll some iterations of the loop and enable some compiler optimizations like vector extension.

How can I reduce big O complexity of two for loops

The function must return the count of pairs of numbers in the array songs (integer array consisting of lengths of songs in seconds) such that the pairs formed add up to whole minutes.
long playlist(int songs_count, int *songs) {
int i, j, k = 0;
for (i = 0; i < songs_count; i++) {
for (j = i + 1; j < songs_count; j++)
if ((songs[i] + songs[j]) % 60 == 0)
k++;
}
return k;
}
A first straight forward approach would be like this:
Create an array holding 60 entries with the remainder of seconds%60 initialized to all zeroes.
Calculate the remainder of each song and increment the related entry in the array.
Iterate over all possible remainders (1..29)
For each remainder you have a = array[i] songs and b = array[60-i] matching songs which you need to combine: num = a*b; k += num;
For i==0 and i==30 you need special handling as the matching song is in same array element: num = a*(a-1);
This will reduce the time complexity to O(N):
You need
a loop over n to populate the array (could be done once when building the song list) and
a loop over 0..30 for the calculation.
This results in O(N)+O(1)
Edit: Depending on your rules (does order of the songs matter) you might need to multiply with 2.
If the value of seconds is less, you can use hash map (unordered_map in c++) to solve it in ~O(n) complexity.
Suppose you know that the maximum value of the pair is 600 secs, then you can have another array storing these values.
for(int i = 1; i <= 10; i++)
{
a[i-1] = i * 60;
}
//a[] = {60, 120, 180, 240, 300, 360, 420, 480, 540, 600};
unordered_map <int, int> mymap;
for(int i = 0; i < songs_count; i++)
{
mymap[i] = songs[i];
}
Now you can modify your code as (Solution is for C++):
long playlist(int songs_count, int *songs)
{
int i, j, k = 0;
for(int i = 1; i <= 10; i++)
{
a[i-1] = i * 60;
}
//a[] = {60, 120, 180, 240, 300, 360, 420, 480, 540, 600};
unordered_map <int, int> mymap;
for(int i = 0; i < songs_count; i++)
{
mymap[i] = songs[i];
}
for (i = 0; i < songs_count; i++)
{
for (j = 0; j < n /*size of a*/; j++)
{
if (a[j] > songs[i] && mymap.find(a[j] - songs[i]) != mymap.end())
{
k++;
}
}
}
return k;
}
You can bring down the complexity from O(N2) to O(N) with a simple approach:
using an array int a[60], for each i in 0..59, compute the number of songs whose duration has i seconds and a whole number of minutes. A single pass is sufficient.
for each i in the array, compute the number of pairs of songs of interest. again a single pass is needed.
to compute the number of pairs, one needs extra specification:
are pairs ordered?
can a pair have twice the same song?
Assuming a pair must have different songs and order does not matter, here is an implementation:
long playlist(int songs_count, int *songs) {
long a[60] = { 0 };
int i;
long total;
for (i = 0; i < songs_count; i++)
a[songs[i] % 60]++;
total = (a[0] * (a[0] - 1) + a[30] * (a[30] - 1)) / 2;
for (i = 1; i <= 29; i++)
total += a[i] * a[60 - i];
return total;
}
If order matters and pairs can have twice the same song, the computation is different:
long playlist(int songs_count, int *songs) {
long a[60] = { 0 };
int i;
long total;
for (i = 0; i < songs_count; i++)
a[songs[i] % 60]++;
total = 0;
for (i = 0; i < 60; i++)
total += a[i] * a[(60 - i) % 60];
return total;
}

Crashing with no errors, sorting algorithm

The purpose of the program is to create a random list of 1000 numbers in an array, sort that array, then find the greatest set of numbers within (x, x+50). The program successfully generates and sorts the numbers within the array, but crashes when the (i, j) set finding algorithm starts. The program generates no errors on compiling, and I'm sure the error is simple, but for the life of me I can't find the issue. Thanks in advance you amazing people!
int main( ){
int a, b, temp, i, j, x, y, tempTotal, arrayStartMax;
int finalTotal = 0;
int *info[ARRAY_FULL];
for (i=0; i<ARRAY_FULL; i++){
info[i]=(int*)malloc(sizeof(int));
*info[i]=rand()%1000;
}
for (a = 0; a < ARRAY_FULL; ++a){
for (b = a + 1; b < ARRAY_FULL; ++b){
if (*info[a] > *info[b]){
temp = *info[a];
*info[a] = *info[b];
*info[b] = temp;
}
}
}
for (i=0; i<ARRAY_FULL; i++){
printf("%d\n", *info[i]);
}
for (i = 0; i <= ARRAY_HALF; i++){
x = *info[i];
y = x+ARRAY_HALF;
tempTotal = 0;
for (j = i; j < i+ARRAY_HALF; i++){
if (*info[j] >= x || *info[j] <= y) {
tempTotal++;
}
if (tempTotal > finalTotal) {
arrayStartMax = *info[i];
finalTotal = tempTotal;
}
}
}
printf("Interval should start at %d for maximum numbers in a set.", arrayStartMax);
}
For the purpose of this program I would like to mention that ARRAY_FULL = 100 and ARRAY_HALF = 50.
Your code is throwing segfault because you're walking i out of bounds in this for loop.
for (j = i; j < i+ARRAY_HALF; i++){
if (*info[j] >= x || *info[j] <= y) {
tempTotal++;
}
if (tempTotal > finalTotal) {
arrayStartMax = *info[i];
finalTotal = tempTotal;
}
You set j = i then increment i prior to the comparison. So j will always be less than i.
Limit i in the comparison section of the for loop and it won't segfault.
I don't think the comparison is doing what you want, but you should be able to find your way home from here.

C - Populate matrix with some density

I've got this code to populate matrix with 0/1 values and RHO density. I need the same for values from 0 to 2. I mean, the percentage of zeros should be the same, but other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho;
}
}
The only thing I've been able to do is something inelegant like this. This leaves zero/non zero percentage inalterate and random modifies the 1 cells:
...
if(grid[cur][i][j] > 0) {
grid[cur][i][j] += rand()%2;
}
I think this code will create 0 with RHO density and other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho ? 0 : rand() % 2 + 1;
}
}

Cost function not decreasing in gradient descent implementation

I am trying implemented batch gradient descent in C language. The problem is, my cost function increases dramatically in every turn and I am not able to understand what is wrong. I checked my code several times and it seems to me that I coded exactly the formulas. Do you have any suggestions or ideas what might be the wrong in the implementation?
My data set is here: https://archive.ics.uci.edu/ml/datasets/Housing
And I reference these slides for the algorithm (I googled this): http://asv.informatik.uni-leipzig.de/uploads/document/file_link/527/TMI04.2_linear_regression.pdf
I read the data set correctly into the main memory. Below part shows how I store the data set information in main memory. It is straight-forward.
//Definitions
#define NUM_OF_ATTRIBUTES 13
#define NUM_OF_SETS 506
#define LEARNING_RATE 0.07
//Data holder
struct data_set_s
{
double x_val[NUM_OF_SETS][NUM_OF_ATTRIBUTES + 1];
double y_val[NUM_OF_SETS];
double teta_val[NUM_OF_ATTRIBUTES + 1];
};
//RAM
struct data_set_s data_set;
Teta values are initialized to 0 and x0 values are initialized to 1.
Below section is the hypothesis function, which is the standart polynomial function.
double perform_hypothesis_a(unsigned short set_index)
{
double result;
int i;
result = 0;
for(i = 0; i < NUM_OF_ATTRIBUTES + 1; i++)
result += data_set.teta_val[i] * data_set.x_val[set_index][i];
return result;
}
Below section is the cost function.
double perform_simplified_cost_func(double (*hypothesis_func)(unsigned short))
{
double result, val;
int i;
result = 0;
for(i = 0; i < NUM_OF_SETS; i++)
{
val = hypothesis_func(i) - data_set.y_val[i];
result += pow(val, 2);
}
result = result / (double)(2 * NUM_OF_SETS);
return result;
}
Below section is the gradient descent function.
double perform_simplified_gradient_descent(double (*hypothesis_func)(unsigned short))
{
double temp_teta_val[NUM_OF_ATTRIBUTES + 1], summation, val;
int i, j, k;
for(i = 0; i < NUM_OF_ATTRIBUTES + 1; i++)
temp_teta_val[i] = 0;
for(i = 0; i < 10; i++) //assume this is "while not converged"
{
for(j = 0; j < NUM_OF_ATTRIBUTES + 1; j++)
{
summation = 0;
for(k = 0; k < NUM_OF_SETS; k++)
{
summation += (hypothesis_func(k) - data_set.y_val[k]) * data_set.x_val[k][j];
}
val = ((double)LEARNING_RATE * summation) / NUM_OF_SETS);
temp_teta_val[j] = data_set.teta_val[j] - val;
}
for(j = 0; j < NUM_OF_ATTRIBUTES + 1; j++)
{
data_set.teta_val[j] = temp_teta_val[j];
}
printf("%lg\n ", perform_simplified_cost_func(hypothesis_func));
}
return 1;
}
While it seems correct to me, when I print the cost function at the end of the every gradient descent, it goes like: 1.09104e+011, 5.234e+019, 2.51262e+028, 1.20621e+037...

Resources