0

I am using a code to plot a particular result in c++. The code uses "matplotlibcpp.h" header function in ubuntu. I had an error in using the header file. because "Python.h" header in this library is not found. So, I tried:

sudo apt-get install python3-dev
sudo apt-get install python2-dev

But, I found no change in the result. I tried to update the Json file of c++ (I am using vs code) and finally managed to compile the program without errors. I have included the paths to python and numpy like this in the 'c_cpp_properties.json' file,

"name": "Linux",
        "includePath": [
            "${workspaceFolder}/**",
            "/usr/include/python2.7",
            "/usr/local/lib/python3.10/dist-packages/numpy/core/include"
        ],

Now, it shows no 'problem'. But I am not getting the result. The terminal response is:

Executing task: C/C++: g++.exe build active file 

Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g "/home/abhiram/Documents/Project Masters/Program files/Frequency noise Measurement/Plots.cpp" -o "/home/abhiram/Documents/Project Masters/Program files/Frequency noise Measurement\Plots.exe"
/bin/sh: 1: C:msys64mingw64bing++.exe: not found

Build finished with error(s).

 *  The terminal process failed to launch (exit code: -1). 
 *  Terminal will be reused by tasks, press any key to close it. 

I have tried to install g++. but, it is also not working. And also,

g++ -o Plots "/home/abhiram/Documents/Project Masters/Program files/Frequency noise Measurement/Plots.cpp"

This shows, the output as:

In file included from /home/abhiram/Documents/Project Masters/Program files/Frequency     noise Measurement/Plots.cpp:4:
/home/abhiram/Documents/Project Masters/Program files/Frequency noise Measurement/matplotlibcpp.h:5:10: fatal error: Python.h: No such file or directory
    5 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.

I am letting you know my whole program also. Kindly drop your request if any other information is required to solve the issue.

#include <iostream>
#include <cmath>
#include <algorithm>
#include "matplotlibcpp.h"
using namespace std;

namespace plt = matplotlibcpp;

// Define sinc function
double sinc(double x) {
    if (x == 0) {
        return 1.0;
    } else {
        return sin(x) / x;
    }
}

int main() {
    // Generate data for the first plot
    constexpr int n_points = 1000;
    double frequencies[n_points];
    double spectrum[n_points];
    double bandwidth = 1 / (2 * M_PI * 1e-12 * 4700);
    for (int i = 0; i < n_points; i++) {
        frequencies[i] = i * 200e6 / (n_points - 1);
        spectrum[i] = abs(sinc(frequencies[i] / bandwidth));
    }
    double max_spectrum = *max_element(spectrum, spectrum + n_points);
    for (int i = 0; i < n_points; i++) {
        spectrum[i] /= max_spectrum;
    }

    // Generate data for the second plot
    constexpr int n_gains = 1000;
    double gains[n_gains];
    double noise_factor[n_gains];
    for (int i = 0; i < n_gains; i++) {
        gains[i] = -20 + i * 40 / (n_gains - 1);
        double noise_figure = pow(10, gains[i] / 10);
        noise_factor[i] = (noise_figure - 1) / noise_figure;
    }

    // Plot the first graph
    plt::loglog(vector<double>(frequencies, frequencies + n_points), vector<double>(spectrum, spectrum + n_points));
    plt::title("Frequency response of a bandpass filter");
    plt::xlabel("Frequency (MHz)");
    plt::ylabel("Normalized amplitude");
    plt::grid(true);
    plt::show();

    // Close the first plot before displaying the second plot
    plt::clf();

    // Plot the second graph
    plt::loglog(vector<double>(gains, gains + n_gains), vector<double>(noise_factor, noise_factor + n_gains));
    plt::title("Noise factor as a function of gain");
    plt::xlabel("Gain (dB)");
    plt::ylabel("Noise factor");
    plt::grid(true);
    plt::show();

    return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
Abhiram C D
  • 107
  • 2
  • 6

1 Answers1

0

You want set the include search path -I/usr/include/python3.10 in the file tasks.json for compiler settings. c_cpp_properties.json is a file for IntelliSense settings. All this is well described in the manual, users just need to follow it carefully.

273K
  • 29,503
  • 10
  • 41
  • 64