0

I'm working on processing signal from microphone. The input I have is just 12 values in list I must preprocess somehow. But the only one solution I find here is just to use multiple if blocks.

mic.init()

while True:
    mic_map = mic.get_map()
    print(len(mic_map[:]))
    b = mic.get_dir(mic_map)
    if b[0]:
        print("-90 degree")
        pass
    elif b[1]:
        print("-60 degree")
        pass
    elif b[2]:
        print("-30 degree")
        pass
    elif b[3]:
        print("0 degree")
        pass
    elif b[4]:
        print("+30 degree")
        pass
    elif b[5]:
        print("+60 degree")
        pass
    elif b[6]:
        print("+90 degree")
        pass
    elif b[7]:
        print("+120 degree")
        pass
    elif b[8]:
        print("+150 degree")
        pass
    elif b[9]:
        print("+-180 degree")
        pass
    elif b[10]:
        print("-150 degree")
        pass
    elif b[11]:
        print("-120 degree")
        pass

Is there any possibilities to make better solution here. Thank you.

UPD. I'm sorry for not providing needed information about some methods.

mic.get_map() is returning a list with 256 values, like an image. And mic.get_dir() returns a list with 12 values in it.

Zhukov Artem
  • 331
  • 1
  • 2
  • 12

1 Answers1

1
mic.init()

while True:
    mic_map = mic.get_map()
    b = mic.get_dir(mic_map)

    for x, values in enumerate(b):
        if values:
             answer = -90 + (30*x)
             if abs(answer) >= 180:
                 answer += 360* (-answer/answer)
             print("{} degrees".format(answer))
             break
Ahmet
  • 434
  • 4
  • 13
  • 2
    I like this! Granted I'm a Python noob, but this looks pretty slick. – ehutchllew Jan 15 '20 at 14:59
  • 1
    Added @ehutchllew improvement – Ahmet Jan 15 '20 at 15:16
  • @brunodesthuilliers given that 'mic_map' is probably not a list, then i am not sure. But if 'b' is a list, then the conditionals make no sense? – Ahmet Jan 15 '20 at 16:19
  • @Ahmet I didn't answer because I'm waiting for the OP to post more details - at least what `b` really looks like. I'm not editing your answer because 1/ I cannot know how I should edit it until we have those details and 2/ it's _your_ answer. Also, I don't see what's rude in asking how you came to the conclusion that your solution would work without knowing what you're dealing with. Do you know something that I don't about what this `mic` object is ? If yes, please share. – bruno desthuilliers Jan 15 '20 at 16:42
  • @brunodesthuilliers Fair point, the mic object is unclear but I gave the best answer I could with the information on hand - even though as you pointed out it isn't correct OP may still salvage it with some modification. – Ahmet Jan 15 '20 at 16:51
  • 1
    @ahmet There's something the Zen of Python that says "in the face of ambiguity, refuse the temptation to guess". It might seem a bit radical at first, but it did save me quite a lot of time over the 20+ past years ;-) – bruno desthuilliers Jan 15 '20 at 17:08
  • @ahmet wow, I like you answer. But, yeah, my bad. mic object is just a class. With dir method I get this: ['__class__', '__name__', 'deinit', 'get_dir', 'get_map', 'init', 'set_led']. Thats unclear for either because there is no way to see about this object in documentation or in source code. Sorry – Zhukov Artem Jan 16 '20 at 06:25
  • @ahmet get_dir() method is return a list with 12 values, and get_map() return a class, but it can give you a list with 256 values, like an image. – Zhukov Artem Jan 16 '20 at 06:37
  • @ZhukovArtem The get_dir() method returns list with 12 values, some of them empty? and if for example b[0] is not empty you want to print "-90 degrees"? – Ahmet Jan 16 '20 at 08:30
  • @Ahmet yeah. The biggest amount of values I get in that list was three from – Zhukov Artem Jan 16 '20 at 08:37
  • @Ahmet Thank you very much. Now it's working as expected – Zhukov Artem Jan 16 '20 at 09:10