Skip to content

Commit

Permalink
Fixed overlapping due to resampling
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
7andahalf committed May 21, 2018
1 parent fed2f9b commit e2e9d6e
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 4 deletions.
3 changes: 3 additions & 0 deletions directdemod/chunker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
It is responsible for creating chunks of the signal and store the info to be used later
It can be helpful for avoiding border issues in filters and demods
'''

# POSSIBLE TODO: automatic distinction in get/set for every cycle. will avoid need of 'uniq'

class chunker:

'''
Expand Down
16 changes: 14 additions & 2 deletions directdemod/comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ def filter(self, filt):
self.updateSignal(filt.applyOn(self.signal))
return self

def bwLim(self, tsampRate, strict = False):
def bwLim(self, tsampRate, strict = False, uniq = "abcd"):

'''Limit the bandwidth by downsampling
Args:
tsampRate (:obj:`int`): target sample rate
strict (:obj:`bool`, optional): if true, the target sample rate will be matched exactly
uniq (:obj:`str`, optional): in case chunked signal, uniq is to differentiate different bwLim funcs
Returns:
:obj:`commSignal`: Updated signal (self)
Expand All @@ -107,12 +108,23 @@ def bwLim(self, tsampRate, strict = False):
raise ValueError("The target sampling rate must be less than current sampling rate")

if strict:

# will be depreciated later on, try not to use

self.__sig = signal.resample(self.signal, int(tsampRate * self.length/self.sampRate))
self.__sampRate = tsampRate
self.__len = len(self.signal)

else:
jumpIndex = int(self.sampRate / tsampRate)
self.__sig = self.signal[0::jumpIndex]

offset = 0
if not self.__chunker is None:
offset = self.__chunker.get(constants.CHUNK_BWLIM + uniq, 0)
nextOff = (jumpIndex - (self.length - offset)%jumpIndex)%jumpIndex
self.__chunker.set(constants.CHUNK_BWLIM + uniq, nextOff)

self.__sig = self.signal[offset::jumpIndex]
self.__sampRate = int(self.sampRate/jumpIndex)
self.__len = len(self.signal)
return self
Expand Down
3 changes: 2 additions & 1 deletion directdemod/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
FLT_BS = 3

## Chunker var names
CHUNK_FREQOFFSET = "freqoffset"
CHUNK_FREQOFFSET = "freqoffset"
CHUNK_BWLIM = "bwlim"
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Experiment 6 - Resampling and chunking"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"during resampling, border issues may arise due to chunking. This experiment is to study those and try to correct them"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os, sys\n",
"nb_dir = os.path.split(os.getcwd())[0]\n",
"if nb_dir not in sys.path:\n",
" sys.path.append(nb_dir)\n",
" \n",
"%matplotlib inline\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from directdemod import chunker, comm\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us try to see if a problem arises. Let us try to resample a given array 'a', downsampling by factor of 4"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96]\n",
"[ 0. 4. 8. 10. 14. 18. 20. 24. 28. 30. 34. 38. 40. 44. 48.\n",
" 50. 54. 58. 60. 64. 68. 70. 74. 78. 80. 84. 88. 90. 94. 98.]\n"
]
}
],
"source": [
"sigOrig = comm.commSignal(40, range(100))\n",
"sigOrig.bwLim(10)\n",
"print(sigOrig.signal)\n",
"\n",
"sigTest = comm.commSignal(40, range(100))\n",
"chunkerObj = chunker.chunker(sigTest, 10)\n",
"sigTestOut = comm.commSignal(10)\n",
"for i in chunkerObj.getChunks:\n",
" sig = comm.commSignal(40, sigTest.signal[i[0]:i[1]]).bwLim(10)\n",
" sigTestOut.extend(sig)\n",
"print(sigTestOut.signal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Clearly they are not the same. In order to correct this, we use the chunker obj. Chunker obj will then store some memory that will help in correcting these border issues."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96]\n",
"[ 0. 4. 8. 12. 16. 20. 24. 28. 32. 36. 40. 44. 48. 52. 56.\n",
" 60. 64. 68. 72. 76. 80. 84. 88. 92. 96.]\n",
"[ True True True True True True True True True True True True\n",
" True True True True True True True True True True True True\n",
" True]\n"
]
}
],
"source": [
"sigOrig = comm.commSignal(40, range(100))\n",
"sigOrig.bwLim(10)\n",
"print(sigOrig.signal)\n",
"\n",
"sigTest = comm.commSignal(40, range(100))\n",
"chunkerObj = chunker.chunker(sigTest, 10)\n",
"sigTestOut = comm.commSignal(10)\n",
"for i in chunkerObj.getChunks:\n",
" sig = comm.commSignal(40, sigTest.signal[i[0]:i[1]], chunkerObj).bwLim(10)\n",
" sigTestOut.extend(sig)\n",
"print(sigTestOut.signal)\n",
"\n",
"print(np.array(sigOrig.signal) == np.array(sigTestOut.signal))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conclusion\n",
"\n",
"The problem was solved, by using the chunker object to retain some memory."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
155 changes: 155 additions & 0 deletions experiments/Experiment 6 - Resampling and chunking.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Experiment 6 - Resampling and chunking"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"during resampling, border issues may arise due to chunking. This experiment is to study those and try to correct them"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os, sys\n",
"nb_dir = os.path.split(os.getcwd())[0]\n",
"if nb_dir not in sys.path:\n",
" sys.path.append(nb_dir)\n",
" \n",
"%matplotlib inline\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from directdemod import chunker, comm\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us try to see if a problem arises. Let us try to resample a given array 'a', downsampling by factor of 4"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96]\n",
"[ 0. 4. 8. 10. 14. 18. 20. 24. 28. 30. 34. 38. 40. 44. 48.\n",
" 50. 54. 58. 60. 64. 68. 70. 74. 78. 80. 84. 88. 90. 94. 98.]\n"
]
}
],
"source": [
"sigOrig = comm.commSignal(40, range(100))\n",
"sigOrig.bwLim(10)\n",
"print(sigOrig.signal)\n",
"\n",
"sigTest = comm.commSignal(40, range(100))\n",
"chunkerObj = chunker.chunker(sigTest, 10)\n",
"sigTestOut = comm.commSignal(10)\n",
"for i in chunkerObj.getChunks:\n",
" sig = comm.commSignal(40, sigTest.signal[i[0]:i[1]]).bwLim(10)\n",
" sigTestOut.extend(sig)\n",
"print(sigTestOut.signal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Clearly they are not the same. In order to correct this, we use the chunker obj. Chunker obj will then store some memory that will help in correcting these border issues."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96]\n",
"[ 0. 4. 8. 12. 16. 20. 24. 28. 32. 36. 40. 44. 48. 52. 56.\n",
" 60. 64. 68. 72. 76. 80. 84. 88. 92. 96.]\n",
"[ True True True True True True True True True True True True\n",
" True True True True True True True True True True True True\n",
" True]\n"
]
}
],
"source": [
"sigOrig = comm.commSignal(40, range(100))\n",
"sigOrig.bwLim(10)\n",
"print(sigOrig.signal)\n",
"\n",
"sigTest = comm.commSignal(40, range(100))\n",
"chunkerObj = chunker.chunker(sigTest, 10)\n",
"sigTestOut = comm.commSignal(10)\n",
"for i in chunkerObj.getChunks:\n",
" sig = comm.commSignal(40, sigTest.signal[i[0]:i[1]], chunkerObj).bwLim(10)\n",
" sigTestOut.extend(sig)\n",
"print(sigTestOut.signal)\n",
"\n",
"print(np.array(sigOrig.signal) == np.array(sigTestOut.signal))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conclusion\n",
"\n",
"The problem was solved, by using the chunker object to retain some memory."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion noaa_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
fmDemdulator = fmDemod.fmDemod()
chunkerObj = chunker.chunker(sigsrc)
for i in chunkerObj.getChunks[:]:
sig = comm.commSignal(constants.IQ_SDRSAMPRATE, sigsrc.read(*i), chunkerObj).offsetFreq(constants.IQ_FREQOFFSET).filter(bhFilter).bwLim(constants.IQ_FMBW).funcApply(fmDemdulator.demod).bwLim(constants.NOAA_AUDSAMPRATE, True)
sig = comm.commSignal(constants.IQ_SDRSAMPRATE, sigsrc.read(*i), chunkerObj).offsetFreq(constants.IQ_FREQOFFSET).filter(bhFilter).bwLim(constants.IQ_FMBW, uniq = "First").funcApply(fmDemdulator.demod).bwLim(constants.NOAA_AUDSAMPRATE, True)
audioOut.extend(sig)

sink.wavFile("out.wav", audioOut).write
Expand Down

0 comments on commit e2e9d6e

Please sign in to comment.