Instrument Neutral Distributed Interface INDI  1.9.5
convolution.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2017 Jasem Mutlaq. All rights reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 *******************************************************************************/
18 
19 #include "convolution.h"
20 #include "indistandardproperty.h"
21 #include "indicom.h"
22 #include "indilogger.h"
23 #include "dsp.h"
24 #include "base64.h"
25 
26 #define _USE_MATH_DEFINES
27 #include <cmath>
28 #include <dirent.h>
29 #include <cerrno>
30 #include <cstring>
31 #include <cstdio>
32 #include <cstdlib>
33 #include <algorithm>
34 #include <thread>
35 #include <chrono>
36 #include <fcntl.h>
37 #include <unistd.h>
38 
39 namespace DSP
40 {
41 
42 Convolution::Convolution(INDI::DefaultDevice *dev) : Interface(dev, DSP_CONVOLUTION, "CONVOLUTION", "Convolution")
43 {
44  IUFillBLOB(&DownloadB, "CONVOLUTION_DOWNLOAD", "Convolution Matrix", "");
45  IUFillBLOBVector(&DownloadBP, &FitsB, 1, m_Device->getDeviceName(), "CONVOLUTION", "Matrix Data", DSP_TAB, IP_RW, 60, IPS_IDLE);
46 }
47 
49 {
50 }
51 
53 {
54  m_Device->defineProperty(&DownloadBP);
56 }
57 
59 {
60  m_Device->deleteProperty(DownloadBP.name);
62 }
63 
64 bool Convolution::ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
65 {
66  if(!strcmp(dev, getDeviceName())) {
67  if(!strcmp(name, DownloadBP.name)) {
68  IUUpdateBLOB(&DownloadBP, sizes, blobsizes, blobs, formats, names, n);
69  LOGF_INFO("Received convolution matrix BLOB for %s", getDeviceName());
70  if(matrix)
71  dsp_stream_free_buffer(matrix);
72  dsp_stream_free(matrix);
73  matrix = loadFITS(blobs[0], sizes[0]);
74  if(matrix != nullptr) {
75  LOGF_INFO("Convolution matrix for %s loaded", getDeviceName());
76  matrix_loaded = true;
77  IDSetBLOB(&DownloadBP, nullptr);
78  }
79  }
80  }
81  return true;
82 }
83 
84 uint8_t* Convolution::Callback(uint8_t *buf, uint32_t dims, int *sizes, int bits_per_sample)
85 {
86  setStream(buf, dims, sizes, bits_per_sample);
87  Convolute();
88  return getStream();
89 }
90 
91 void Convolution::Convolute()
92 {
93  if(matrix_loaded)
95 }
96 
97 Wavelets::Wavelets(INDI::DefaultDevice *dev) : Interface(dev, DSP_CONVOLUTION, "WAVELETS", "Wavelets")
98 {
99  for(int i = 0; i < N_WAVELETS; i++) {
100  char strname[MAXINDINAME];
101  char strlabel[MAXINDILABEL];
102  sprintf(strname, "WAVELET%0d", i);
103  sprintf(strlabel, "%d pixels Gaussian Wavelet", (i+1)*3);
104  IUFillNumber(&WaveletsN[i], strname, strlabel, "%3.3f", -15.0, 255.0, 1.0, 0.0);
105  }
106  IUFillNumberVector(&WaveletsNP, WaveletsN, N_WAVELETS, m_Device->getDeviceName(), "WAVELET", "Wavelets", DSP_TAB, IP_RW, 60, IPS_IDLE);
107 }
108 
110 {
111 }
112 
114 {
115  m_Device->defineProperty(&WaveletsNP);
117 }
118 
120 {
121  m_Device->deleteProperty(WaveletsNP.name);
123 }
124 
125 bool Wavelets::ISNewNumber(const char *dev, const char *name, double *values, char *names[], int n)
126 {
127  if (!strcmp(dev, getDeviceName()) && !strcmp(name, WaveletsNP.name)) {
128  IUUpdateNumber(&WaveletsNP, values, names, n);
129  IDSetNumber(&WaveletsNP, nullptr);
130  }
131  return true;
132 }
133 
134 uint8_t* Wavelets::Callback(uint8_t *buf, uint32_t dims, int *sizes, int bits_per_sample)
135 {
136  setStream(buf, dims, sizes, bits_per_sample);
137  double min = dsp_stats_min(stream->buf, stream->len);
138  double max = dsp_stats_max(stream->buf, stream->len);
140  for (int i = 0; i < WaveletsNP.nnp; i++) {
141  int size = (i+1)*3;
143  dsp_stream_p matrix = dsp_stream_new();
144  dsp_stream_add_dim(matrix, size);
145  dsp_stream_add_dim(matrix, size);
146  dsp_stream_alloc_buffer(matrix, matrix->len);
147  for(int y = 0; y < size; y++) {
148  for(int x = 0; x < size; x++) {
149  matrix->buf[x + y * size] = sin(static_cast<double>(x)*M_PI/static_cast<double>(size))*sin(static_cast<double>(y)*M_PI/static_cast<double>(size));
150  }
151  }
152  dsp_convolution_convolution(tmp, matrix);
153  dsp_buffer_sub(tmp, matrix->buf, matrix->len);
154  dsp_buffer_mul1(tmp, WaveletsNP.np[i].value/8.0);
155  dsp_buffer_sum(out, tmp->buf, tmp->len);
156  dsp_buffer_normalize(tmp->buf, min, max, tmp->len);
157  dsp_stream_free_buffer(matrix);
158  dsp_stream_free(matrix);
160  dsp_stream_free(tmp);
161  }
164  stream = dsp_stream_copy(out);
165  return getStream();
166 }
167 }
DSP::Convolution::Convolution
Convolution(INDI::DefaultDevice *dev)
Definition: convolution.cpp:42
DSP::Interface::loadFITS
dsp_stream_p loadFITS(char *buf, int len)
loadFITS Converts FITS data into a dsp_stream structure pointer.
Definition: dspinterface.cpp:333
DSP::Interface
Definition: dspinterface.h:43
indistandardproperty.h
dsp.h
dsp_buffer_sum
void dsp_buffer_sum(dsp_stream_p stream, dsp_t *in, int inlen)
Sum elements of one stream to another's.
Definition: buffer.c:65
dsp_stream_new
DLL_EXPORT dsp_stream_p dsp_stream_new()
Allocate a new DSP stream type.
Definition: stream.c:48
min
double min(void)
DSP::Convolution::Callback
uint8_t * Callback(uint8_t *out, uint32_t dims, int *sizes, int bits_per_sample) override
Callback Called by processBLOB.
Definition: convolution.cpp:84
indicom.h
Implementations for common driver routines.
DSP::Interface::getStream
uint8_t * getStream()
Definition: dspinterface.cpp:699
DSP::Wavelets::Activated
void Activated() override
Activated Called after activation from client application.
Definition: convolution.cpp:113
_INumberVectorProperty::nnp
int nnp
Definition: indiapi.h:336
MAXINDINAME
#define MAXINDINAME
Definition: indiapi.h:190
IUFillNumber
void IUFillNumber(INumber *np, const char *name, const char *label, const char *format, double min, double max, double step, double value)
Assign attributes for a number property. The number's auxiliary elements will be set to NULL.
Definition: indidriver.c:348
convolution.h
INDI::DefaultDevice::defineProperty
void defineProperty(INumberVectorProperty *property)
Definition: defaultdevice.cpp:997
DSP::Convolution::ISNewBLOB
bool ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) override
Definition: convolution.cpp:64
DSP::Interface::setStream
void setStream(void *buf, uint32_t dims, int *sizes, int bits_per_sample)
Definition: dspinterface.cpp:665
dsp_buffer_normalize
#define dsp_buffer_normalize(buf, len, mn, mx)
Normalize the input stream to the minimum and maximum values.
Definition: dsp.h:594
DSP::Convolution::~Convolution
~Convolution()
Definition: convolution.cpp:48
DSP::Interface::Deactivated
virtual void Deactivated()
Deactivated Called after deactivation from client application.
Definition: dspinterface.cpp:226
INDI::BaseDevice::getDeviceName
const char * getDeviceName() const
Definition: basedevice.cpp:799
IDSetBLOB
void void void void void void void void void IDSetBLOB(const IBLOBVectorProperty *b, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Tell client to update an existing BLOB vector property.
DSP::Interface::FitsB
IBLOB FitsB
Definition: dspinterface.h:156
DSP::Wavelets::~Wavelets
~Wavelets()
Definition: convolution.cpp:109
dsp_stream_free
DLL_EXPORT void dsp_stream_free(dsp_stream_p stream)
Free the DSP stream passed as argument.
Definition: stream.c:76
max
double max(void)
dsp_stream_t
Contains a set of informations and data relative to a buffer and how to use it.
Definition: dsp.h:175
dsp_convolution_convolution
dsp_stream_p dsp_convolution_convolution(dsp_stream_p stream, dsp_stream_p object)
A cross-convolution processor.
Definition: convolution.c:45
DSP::Wavelets::Deactivated
void Deactivated() override
Deactivated Called after deactivation from client application.
Definition: convolution.cpp:119
dsp_stats_min
#define dsp_stats_min(buf, len)
Gets the minimum value of the input stream.
Definition: dsp.h:382
indilogger.h
dsp_stream_copy
DLL_EXPORT dsp_stream_p dsp_stream_copy(dsp_stream_p stream)
Create a copy of the DSP stream passed as argument.
Definition: stream.c:89
DSP::Interface::stream
dsp_stream_p stream
Definition: dspinterface.h:178
IUFillNumberVector
void IUFillNumberVector(INumberVectorProperty *nvp, INumber *np, int nnp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a number vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidriver.c:455
base64.h
_INumberVectorProperty::np
INumber * np
Definition: indiapi.h:334
IPS_IDLE
@ IPS_IDLE
Definition: indiapi.h:160
dsp_stream_t::len
int len
The buffers length.
Definition: dsp.h:178
_INumberVectorProperty::name
char name[MAXINDINAME]
Definition: indiapi.h:322
dsp_stats_max
#define dsp_stats_max(buf, len)
Gets the maximum value of the input stream.
Definition: dsp.h:398
IUFillBLOB
void IUFillBLOB(IBLOB *bp, const char *name, const char *label, const char *format)
Assign attributes for a BLOB property. The BLOB's data and auxiliary elements will be set to NULL.
Definition: indidriver.c:390
dsp_stream_free_buffer
DLL_EXPORT void dsp_stream_free_buffer(dsp_stream_p stream)
Free the buffer of the DSP Stream passed as argument.
Definition: stream.c:41
LOGF_INFO
#define LOGF_INFO(fmt,...)
Definition: indilogger.h:82
DSP::DSP_TAB
const char * DSP_TAB
Definition: dspinterface.cpp:58
DSP::Interface::Activated
virtual void Activated()
Activated Called after activation from client application.
Definition: dspinterface.cpp:221
DSP
The DSP Namespace adds signal processing to INDI drivers. Primarily written for sensors and detectors...
Definition: convolution.cpp:39
DSP::Wavelets::Callback
uint8_t * Callback(uint8_t *out, uint32_t dims, int *sizes, int bits_per_sample) override
Callback Called by processBLOB.
Definition: convolution.cpp:134
dsp_stream_alloc_buffer
DLL_EXPORT void dsp_stream_alloc_buffer(dsp_stream_p stream, int len)
Allocate a buffer with length len on the stream passed as argument.
Definition: stream.c:21
name
const char * name
Definition: indiserver.c:116
N_WAVELETS
#define N_WAVELETS
Definition: convolution.h:26
IUUpdateNumber
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
Definition: indidriver.c:225
_IBLOBVectorProperty::name
char name[MAXINDINAME]
Definition: indiapi.h:474
IP_RW
@ IP_RW
Definition: indiapi.h:185
MAXINDILABEL
#define MAXINDILABEL
Definition: indiapi.h:191
IUUpdateBLOB
int IUUpdateBLOB(IBLOBVectorProperty *bvp, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
Update all BLOB members in a BLOB vector property.
Definition: indidriver.c:285
DSP::Convolution::Activated
void Activated() override
Activated Called after activation from client application.
Definition: convolution.cpp:52
DSP::Wavelets::Wavelets
Wavelets(INDI::DefaultDevice *dev)
Definition: convolution.cpp:97
DSP::Wavelets::ISNewNumber
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Definition: convolution.cpp:125
dsp_stream_add_dim
DLL_EXPORT void dsp_stream_add_dim(dsp_stream_p stream, int len)
Add a dimension with length len to a DSP stream.
Definition: stream.c:111
INDI::DefaultDevice
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
Definition: defaultdevice.h:118
IUFillBLOBVector
void IUFillBLOBVector(IBLOBVectorProperty *bvp, IBLOB *bp, int nbp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a BLOB vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidriver.c:499
INDI::DefaultDevice::deleteProperty
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
Definition: defaultdevice.cpp:965
IDSetNumber
void void void IDSetNumber(const INumberVectorProperty *n, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Tell client to update an existing number vector property.
DSP::Convolution::Deactivated
void Deactivated() override
Deactivated Called after deactivation from client application.
Definition: convolution.cpp:58
dsp_buffer_sub
void dsp_buffer_sub(dsp_stream_p stream, dsp_t *in, int inlen)
Subtract elements of one stream from another's.
Definition: buffer.c:54
dsp_buffer_mul1
void dsp_buffer_mul1(dsp_stream_p stream, double val)
Multiply elements of the input stream to a value.
Definition: buffer.c:170
dsp_stream_t::buf
dsp_t * buf
buffer
Definition: dsp.h:184
DSP::Interface::m_Device
INDI::DefaultDevice * m_Device
Definition: dspinterface.h:172
DSP::Interface::getDeviceName
const char * getDeviceName()
Definition: dspinterface.cpp:83